JS: Property key "constructor"
Purpose
The property "constructor" is meant to let you find the creator function of a object.
(but is not reliable.)
Here is a typical way constructor property is used.
// create a constructor function function Burger() {} // create a object const jj = new Burger(); // typical use of constructor property to find its creator function console.assert(jj.constructor === Burger);
Value of the property key "constructor"
Normally, it is a function that creates the object.
But it can be changed by user.
Example. Changing parent defeats constructor property
function Burger() {} const xobj = new Burger(); console.assert(xobj["constructor"] === Burger); // change parent Reflect.setPrototypeOf(xobj, Array.prototype); // same object, but constructor no longer Burger console.assert(xobj["constructor"] === Array);
Example. Adding a constructor property defeats constructor property
// create a object const xobj = {}; console.assert(xobj["constructor"] === Object); // set a property key constructor xobj.constructor = RegExp; // same object, but constructor no longer Object console.assert(xobj["constructor"] === RegExp);
Where does the constructor property came from?
By spec, every function has a
property key "prototype".
(except Arrow Function )
For user-defined function f, the value of f.prototype by default is {"constructor":f} (that is, a object with one property key "constructor", with value of the function itself).
// every function has a property key "prototype" (except arrow fuction) function Burger() {} console.assert( Object.hasOwn(Burger, "prototype"), ); // its value is a object, with just 1 property console.assert( Object.getOwnPropertyNames(Burger.prototype).length === 1, ); // the property key is the string "constructor" console.assert( Object.hasOwn(Burger.prototype, "constructor"), ); // value of the property key "constructor" is the function console.assert( Burger.prototype.constructor === Burger, );
When you create a object using new as in jj = new Burger(), the newly created object jj does not have a property key "constructor", but inherits from its parent, which is Burger.prototype.
Let's be more precise:
- Let there be a function Burger, whose return value is not an object. (that is, no
returnstatement, or explicitlyreturna non-object. [see Object Type]) - The function Burger is used with “new” operator, as in
jj = new Burger()to create a object. - The parent of jj, has a property key
"constructor".
function Burger() {} const jj = new Burger(); console.assert( Object.hasOwn(jj, "constructor") === false, ); console.assert( Object.hasOwn(Reflect.getPrototypeOf(jj), "constructor"), );
🟢 tip: do not use the constructor property
There's no reliable way to find a object's creator function. Because:
- The value of the property key
"constructor"can be changed anytime. - User can add a property key
"constructor"to an object or change its value. - The parent object of a object can change anytime. [see Get Set Prototype]
- JavaScript objects are often created without a constructor. [see Create Object]
[see What is Constructor?]