Constructor property in JavaScript
In JavaScript, every object has a special property called constructor which refers to the functions that was used to create the object
When an object is created using the constructor function, the constructor property of that object is set to the constructor function that was used to create it.
Let's see an example.
function Person(name, age) {
this.name = name;
this.age = age;
}
const john = new Person("John", 23);
console.log(john.constructor);
Output
f Person(name, age) {
this.name = name;
this.age = age;
}
Here, `john` is an object created using the `Person` constructor function. The `constructor` property of `john` refers to the `Person` function.
Note that the value of this property is a reference to the function itself, not a string containing the function's name.
We can also use the `constructor` property to create new objects of the same type as the original object. Example given below.
const jane = new john.constructor("Jane", 25);
console.log(jane);
console.log(jane.constructor);
Output
Person {name: 'Jane', age: 25}
f Person(name, age) {
this.name = name;
this.age = age;
}
In JavaScript, every funcion is also an object, and as such, it also has a `constructor` property.
In the case of the `Person` function from the previous example, its `constructor` property would refer to the `Function` constructor.
This is because the `Person` function is itself a constructor function, and was created using the `Function` constructor.
Function() constructor
The Function() constructor creates a new Function object. Calling the constructor directly can create functions dynamically, but suffers from security and performance issues.
Function constructor creates functions which execute in the global scope only.
const prod = new Function('n1', 'n2', 'return n1 * n2');
console.log(prod(6, 9));
Output
54
All arguments passed to the function, except the last, are treated as the names of identifiers of the parameters in the function to be created, in the order in which they're passed.
Now that you've understood the Function constructor, let's go back to where we left.
function Person(name, age) {
this.name = name;
this.age = age;
}
console.log(Person.constructor);
Output
ƒ Function() { [native code] }
In the case of the `Person` function, its `constructor` property would refer to the `Function` constructor.
`[native code]` indicates that the implementation of the `Function` constructor is written in native code, such as C, C++ rather than JavaScript.
This is because the `Function` constructor is a fundamental part of the JavaScript language, and it is implemented in the JavaScript engine itself, which is typically written in a lower-level language for performance reasons.
The `Function` constructor is itself a built in JavaScript function, and like every other function, it has a `constructor` property that refers to the constructor function that was used to create it. In this case, the `constructor` property of the `Function` constructor refers to the `Function` constructor itself.
In other words, the `Function` constructor was created using the `Function` constructor, so its `constructor` property points back to itself.
console.log(Function.constructor === Function);
Output
true