A Complete Guide To JavaScript Contructor Functions
A Complete Guide To JavaScript Contructor Functions
Donate Now
(https://www.javascripttutorial.net/donation/)
For example, the following creates a new person object with two properties firstName and
lastName :
let person = {
firstName: 'John',
lastName: 'Doe'
};
In practice, you often need to create many similar objects like the person object.
https://www.javascripttutorial.net/javascript-constructor-function/ 1/7
29/07/2022, 08:09 A Complete Guide to JavaScript Contructor Functions
To do that, you can use a constructor function to define a custom type and the new operator to
create multiple objects from this type.
The name of a constructor function starts with a capital letter like Person , Document , etc.
keyword that allows you to define a custom type. And classes are just syntactic sugar over the
constructor functions with some enhancements.
this.firstName = firstName;
this.lastName = lastName;
In this example, the Person is the same as a regular function except that its name starts with the
capital letter P .
To create a new instance of the Person , you use the new operator:
Assign the arguments 'John' and 'Doe' to the firstName and lastName properties of
the object.
https://www.javascripttutorial.net/javascript-constructor-function/ 2/7
29/07/2022, 08:09 A Complete Guide to JavaScript Contructor Functions
// this = {};
this.firstName = firstName;
this.lastName = lastName;
// return this;
let person = {
firstName: 'John',
lastName: 'Doe'
};
However, the constructor function Person allows you to create multiple similar objects. For example:
An object may have methods that manipulate its data. To add a method to an object created via the
constructor function, you can use the this keyword. For example:
https://www.javascripttutorial.net/javascript-constructor-function/ 3/7
29/07/2022, 08:09 A Complete Guide to JavaScript Contructor Functions
this.firstName = firstName;
this.lastName = lastName;
this.getFullName = function () {
};
Now, you can create a new Person object and invoke the getFullName() method:
console.log(person.getFullName());
Output:
John Doe
The problem with the constructor function is that when you create multiple instances of the Person ,
the this.getFullName() is duplicated in every instance, which is not memory efficient.
To resolve this, you can use the prototype (https://www.javascripttutorial.net/javascript-prototype/) so that all
instances of a custom type can share the same methods.
Typically, a constructor function implicitly returns this that set to the newly created object. But if it
has a return statement, then here’s are the rules:
If return is called with an object, the constructor function returns that object instead of this .
Technically, you can call a constructor function like a regular function without using the new keyword
like this:
In this case, the Person just executes like a regular function. Therefore, the this inside the
Person function doesn’t bind to the person variable but the global object
(https://www.javascripttutorial.net/es-next/javascript-globalthis/) .
If you attempt to access the firstName or lastName property, you’ll get an error:
console.log(person.firstName);
Error:
Similarly, you cannot access the getFullName() method since it’s bound to the global object.
person.getFullName();
Error:
To prevent a constructor function to be invoked without the new keyword, ES6 introduced the
new.target (https://www.javascripttutorial.net/es6/javascript-new-target/) property.
If a constructor function is called with the new keyword, the new.target returns a reference of the
function. Otherwise, it returns undefined .
The following adds a statement inside the Person function to show the new.target to the
console:
https://www.javascripttutorial.net/javascript-constructor-function/ 5/7
29/07/2022, 08:09 A Complete Guide to JavaScript Contructor Functions
console.log(new.target);
this.firstName = firstName;
this.lastName = lastName;
this.getFullName = function () {
};
The following returns undefined because the Person constructor function is called like a regular
function:
Output:
undefined
However, the following returns a reference to the Person function because it’s called with the new
keyword:
Output:
[Function: Person]
By using the new.target , you can force the callers of the constructor function to use the new
keyword. Otherwise, you can throw an error like this:
https://www.javascripttutorial.net/javascript-constructor-function/ 6/7
29/07/2022, 08:09 A Complete Guide to JavaScript Contructor Functions
if (!new.target) {
this.firstName = firstName;
this.lastName = lastName;
Alternatively, you can make the syntax more flexible by creating a new Person object if the users of
the constructor function don’t use the new keyword:
if (!new.target) {
this.firstName = firstName;
this.lastName = lastName;
console.log(person.firstName);
This pattern is often used in JavaScript libraries and frameworks to make the syntax more flexible.
Summary
JavaScript constructor function is a regular function used to create multiple similar objects.
https://www.javascripttutorial.net/javascript-constructor-function/ 7/7