🌐
Scribd
scribd.com › document › 621321199 › A-Complete-Guide-to-JavaScript-Contructor-Functions
A Complete Guide To JavaScript Contructor Functions | PDF | Constructor (Object Oriented Programming) | Programming
This document provides a complete guide to JavaScript constructor functions. It explains that constructor functions allow you to define a custom type and use the new operator to create multiple objects from that type.
Discussions

Why in JavaScript is a function considered both a constructor and an object? - Stack Overflow
The latter has the innate knowledge on how to use the prototype property of the constructor, construct an object with proper internal links to the prototype, and call the constructor function on it setting up this correctly. If you look into the ECMAScript standard (PDF) you will see that all ... More on stackoverflow.com
🌐 stackoverflow.com
Constructor concept in JavaScript - Stack Overflow
In one of my questions, I got the following code as one of the answers. My understanding of the language has come has come a far better now, just have one small question. var person = function() {... More on stackoverflow.com
🌐 stackoverflow.com
Object Constructor function with .prototype method in JavaScript
.prototype isn’t a method, but an object. The constructor is Person. To access the object instance, use this. What you wrote above with Person.prototype.nationality = "English" had nothing to do with methods. It is just adding a string property that can be accessed by every instance of Person. It’s valid JS code, but not that useful. What you can do instead is: Person.prototype.fullName = function() { return this.firstName + " " + this.lastName; } More on reddit.com
🌐 r/learnjavascript
4
3
April 11, 2024
[AskJS] Factory functions vs. Constructors. Why choose factory functions?
See this explanation for some good reasons: https://www.reddit.com/r/javascript/s/SquoeVYzoK More on reddit.com
🌐 r/javascript
24
38
March 13, 2024
🌐
W3Schools
w3schools.com › js › js_object_constructors.asp
JavaScript Object Constructors
Function Path Function Intro Function Invocation Function Parameters Function Returns Function Arguments Function Expressions Function Arrow Function Quiz ... Object Path Object Intro Object Properties Object Methods Object this Object Display Object Constructors
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › objects: the basics
Constructor, operator "new"
The “capital letter first” is a common agreement, to make it clear that a function is to be run with new. ... If we have many lines of code all about creation of a single complex object, we can wrap them in an immediately called constructor function, like this:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Function › Function
Function() constructor - JavaScript - MDN Web Docs
January 21, 2026 - The Function() constructor creates Function objects. Calling the constructor directly can create functions dynamically, but suffers from security and similar (but far less significant) performance issues as eval(). However, unlike eval (which may have access to the local scope), the Function ...
🌐
Programiz
programiz.com › javascript › constructor-function
JavaScript Constructor Function (with Examples)
The JavaScript constructor function creates and initializes objects. In this tutorial, you will learn about JavaScript constructor functions with the help of examples.
🌐
Medium
medium.com › @halfcircassian › constructor-functions-in-javascript-101-a7123efbc0b6
Constructor Functions in JavaScript 101 | by Sıla Özeren | Medium
October 30, 2024 - If you’re familiar with object-oriented programming (OOP), constructor functions are similar to “classes” in other languages, like Python or Java. In this guide, I will break down how constructor functions work in JavaScript and build a simple example to demonstrate their functionality.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › js-constructor-method
JavaScript Constructor Method - GeeksforGeeks
A constructor is a special function used to create and initialize objects, defined using the function keyword or class syntax. The new keyword triggers the constructor, creating a new object and setting this to refer to it.
Published   2 weeks ago
Find elsewhere
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript constructor function
JavaScript Constructor Function
December 17, 2023 - Summary: in this tutorial, you’ll learn about the JavaScript constructor function and how to use the new keyword to create an object.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-function-constructor
JavaScript Function() Constructor - GeeksforGeeks
July 23, 2025 - The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string.
Top answer
1 of 8
14

There is nothing magical about functions and constructors. All objects in JavaScript are … well, objects. But some objects are more special than the others: namely built-in objects. The difference lies mostly in following aspects:

  1. General treatment of objects. Examples:
    • Numbers and Strings are immutable (⇒ constants). No methods are defined to change them internally — new objects are always produced as the result. While they have some innate methods, you cannot change them, or add new methods. Any attempts to do so will be ignored.
    • null and undefined are special objects. Any attempt to use a method on these objects or define new methods causes an exception.
  2. Applicable operators. JavaScript doesn't allow to (re)define operators, so we stuck with what's available.
    • Numbers have a special way with arithmetic operators: +, -, *, /.
    • Strings have a special way to handle the concatenation operator: +.
    • Functions have a special way to handle the "call" operator: (), and the new operator. The latter has the innate knowledge on how to use the prototype property of the constructor, construct an object with proper internal links to the prototype, and call the constructor function on it setting up this correctly.

If you look into the ECMAScript standard (PDF) you will see that all these "extra" functionality is defined as methods and properties, but many of them are not available to programmers directly. Some of them will be exposed in the new revision of the standard ES3.1 (draft as of 15 Dec 2008: PDF). One property (__proto__) is already exposed in Firefox.

Now we can answer your question directly. Yes, a function object has properties, and we can add/remove them at will:

var fun = function(){/* ... */};
fun.foo = 2;
console.log(fun.foo);  // 2
fun.bar = "Ha!";
console.log(fun.bar);  // Ha!

It really doesn't matter what the function actually does — it never comes to play because we don't call it! Now let's define it:

fun = function(){ this.life = 42; };

By itself it is not a constructor, it is a function that operates on its context. And we can easily provide it:

var context = {ford: "perfect"};

// now let's call our function on our context
fun.call(context);

// it didn't create new object, it modified the context:
console.log(context.ford);           // perfect
console.log(context.life);           // 42
console.log(context instanceof fun); // false

As you can see it added one more property to the already existing object.

In order to use our function as a constructor we have to use the new operator:

var baz = new fun();

// new empty object was created, and fun() was executed on it:
console.log(baz.life);           // 42
console.log(baz instanceof fun); // true

As you can see new made our function a constructor. Following actions were done by new:

  1. New empty object ({}) was created.
  2. Its internal prototype property was set to fun.prototype. In our case it will be an empty object ({}) because we didn't modify it in any way.
  3. fun() was called with this new object as a context.

It is up to our function to modify the new object. Commonly it sets up properties of the object, but it can do whatever it likes.

Fun trivia:

  • Because the constructor is just an object we can calculate it:

    var A = function(val){ this.a = val; };
    var B = function(val){ this.b = val; };
    var C = function(flag){ return flag ? A : B; };
    
    // now let's create an object:
    var x = new (C(true))(42);
    
    // what kind of object is that?
    console.log(x instanceof C); // false
    console.log(x instanceof B); // false
    console.log(x instanceof A); // true
    // it is of A
    
    // let's inspect it
    console.log(x.a); // 42
    console.log(x.b); // undefined
    
    // now let's create another object:
    var y = new (C(false))(33);
    
    // what kind of object is that?
    console.log(y instanceof C); // false
    console.log(y instanceof B); // true
    console.log(y instanceof A); // false
    // it is of B
    
    // let's inspect it
    console.log(y.a); // undefined
    console.log(y.b); // 33
    
    // cool, heh?
    
  • Constructor can return a value overriding the newly created object:

    var A = function(flag){
      if(flag){
        // let's return something completely different
        return {ford: "perfect"};
      }
      // let's modify the object
      this.life = 42;
    };
    
    // now let's create two objects:
    var x = new A(false);
    var y = new A(true);
    
    // let's inspect x
    console.log(x instanceof A); // true
    console.log(x.ford);         // undefined
    console.log(x.life);         // 42
    
    // let's inspect y
    console.log(y instanceof A); // false
    console.log(y.ford);         // perfect
    console.log(y.life);         // undefined
    

    As you can see x is of A with the prototype and all, while y is our "naked" object we returned from the constructor.

2 of 8
12

Your understanding is wrong:

myFunction().myProperty; // myFunction has no properties

The reason it does not work is because ".myProperty" is applied to the returned value of "myFunction()", not to the object "myFunction". To wit:

$ js
js> function a() { this.b=1;return {b: 2};}
js> a().b
2
js> 

Remember, "()" is an operator. "myFunction" is not the same as "myFunction()". You don't need a "return" when instanciang with new:

js> function a() { this.b=1;}
js> d = new a();
[object Object]
js> d.b;
1
🌐
Medium
medium.com › @hridoymahmud › understanding-javascript-constructors-function-vs-class-63d140a44288
Understanding JavaScript Constructors: Function vs. Class | by Mahmudur Rahman | Medium
July 11, 2024 - Understanding JavaScript Constructors: Function vs. Class In the world of JavaScript, creating and initializing objects is a fundamental task. Two primary ways to achieve this are through constructor …
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › constructor
Object.prototype.constructor - JavaScript - MDN Web Docs
The constructor data property of an Object instance returns a reference to the constructor function that created the instance object. Note that the value of this property is a reference to the function itself, not a string containing the function's name.
🌐
Codecademy
codecademy.com › docs › javascript › constructors
JavaScript | Constructors | Codecademy
June 26, 2024 - To use a constructor function to create objects, simply define a JavaScript function with any number of arguments. Inside the function, the keyword this is used as a placeholder for the object being created.
🌐
Medium
medium.com › @mohdtalib.dev › a-beginners-guide-to-constructors-in-javascript-from-functions-to-classes-85abfa0701a9
A Beginner’s Guide to Constructors in JavaScript: From Functions to Classes. | by devtalib | Medium
January 25, 2024 - Here, Person is a constructor function that initializes new objects with name and age properties and a describe method. When we create a new instance of Person, these properties and methods are attached to the person1 object. In JavaScript, with the introduction of ES6 (ECMAScript 2015), the concept of classes was added as a part of its syntax, Within these classes, the constructor method became a key player.
🌐
Scaler
scaler.com › home › topics › javascript › javascript constructor function
JavaScript Constructor Function - Scaler Topics
October 26, 2023 - With this article by Scaler Topics, What, why, and how of Constructors in Javascript with easy-to-understand theoretical explanations and coding examples.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-object-constructors
JavaScript Object Constructors - GeeksforGeeks
January 20, 2026 - //Driver Code Starts // Constructor ... //Driver Code Ends · The this keyword in JavaScript refers to the object associated with the current execution context, similar to OOP languages like C++, C#, and Java, and its value ...
🌐
Talent500
talent500.com › blog › javascript-constructors-comprehensive-guide
JavaScript Constructors: A Comprehensive Guide for Developers
January 29, 2025 - ... Object: Creates a general-purpose object. Array: Creates an array. Date: Creates a date object. Boolean: Creates a new boolean object. RegExp: Creates a regular expression object. Function: Creates a new function.
Top answer
1 of 5
16

First of all the person is a regular JavaScript function. When you call it, of course, lines:

this.firstName = "";
this.lastName = "";

are executed. Constructor function is rather a concept than a something really existing in the JS language. You need constructors to create new similar objects by calling new MyCtr(). At the same time you need regular functions to encapsulate piece of logic and make it reusable in different places without copy/paste the code.

You can use all functions in JavaScript as a constructor. Just add new keyword in front of function call expression. This thing changes the context of execution of the function. Without new the function is executed against global object (window in a browser). And this variable inside the function refers to the context.

Not every function is ready to be a constructor. Usually, constructor functions are doing something with this variable which is a reference to an object which is created during new MyCtr() call. Also, constructor functions never return a value.

Lets look at few examples (you can execute it directly in the browser's console):

function foo() {
    this.a = 1;
}

foo();  // using function as a regular function. Ctx is window.
console.log(window.a);  // prints "1"
foo.call(window);  // explicitly specify execution ctx. The same as just foo() call

var instance = new foo();  // using foo as a constructor
console.log(instance.a);   // prints "1"

// actually you can do it without new keyword
var instance = {};  // manually create new object
foo.call(instance); // manually call foo against this object
console.log(instance.a);   // prints "1"

// However, the code above is not strictly equivalent to the code using new. 
// It omits the concept of prototype, but it's enough for our current task.

Regarding functions and files. There is no such thing in the language like in the Java that each class must be placed in the separate file. You can put all your functions within one file and then use it as constructors or not. However, the best practice is to reside one constructor function (read as class) per one file (called module).

2 of 5
8

any function in JavaScript can act as a constructor when the function is invoked with new operator.

Now, what a constructor does ? it creates/instantiate an object from the constructor function. like its shown in the below image.

LINK , it explain the fundamentals very clearly.

what is this ?

when this constructor function is invoked with new, this points to the new object created at that invocation. and in that object we set firtName and lastName (it is the initialization of the new object created).

Now when we add methods to the prototype of the constructor , that is being shared between all the objects created using the constructor function(picture explains it lit bit more)

and regarding your last query "And also in one of the blogs I was studying if the file name is Samplescript.js and if a function is written using the same name inside this like var Samplescript=function(){}, will this function be considered a constructor? Please clarify me this"

any function in JavaScript can act as a constructor when the function is invoked with new operator, and its not the way that blog says.

so please stop reading that blog, the link i provided is a very good starting point

🌐
Kevin Chisholm
blog.kevinchisholm.com › javascript › the-javascript-this-keyword-deep-dive-constructor-functions
The JavaScript “this” Keyword Deep Dive: Constructor Functions | Kevin Chisholm - Blog
February 10, 2021 - We’ve also created a constructor function named “Foo”. When we instantiate Foo, we assign that instantiation to the variable: “bar”. In other words, the variable “bar” becomes an instance of “Foo”. This is a very important point. When you instantiate a JavaScript constructor function, the JavaScript “this” keyword refers to the instance of the constructor.