JS: constructor (property)

By Xah Lee. Date: . Last updated: .

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 Fcon() {}

console.assert(Object.hasOwn(Fcon, "prototype"));

// its value is a object, with just 1 property
console.assert(Object.getOwnPropertyNames(Fcon.prototype).length === 1);

// the property key is the string "constructor"
console.assert(Object.hasOwn(Fcon.prototype, "constructor"));

// value of the property key "constructor" is the function
console.assert(Fcon.prototype.constructor === Fcon);

When you create a object using new as in jj = new Fcon(), the newly created object jj does not have a property key "constructor", but inherits from its parent, which is Fcon.prototype.

Let's be more precise:

  1. Let there be a function Fcon, whose return value is not a object. (that is, no return statement, or explicitly return a non-object. 〔see Object Type〕)
  2. The function Fcon is used with new (operator), as in jj = new Fcon() to create a object.
  3. The parent of jj, has a property key "constructor".
function Fcon() {}
const jj = new Fcon();
console.assert(Object.hasOwn(jj, "constructor") === false);
console.assert(Object.hasOwn(Reflect.getPrototypeOf(jj), "constructor"));

Purpose

The constructor property 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 Fcon() {}

// create a object
const jj = new Fcon();

// typical use of constructor property to find its creator function
console.log(jj.constructor);
// [Function: Fcon]

Value of the Property Key "constructor"

Normally, it is a function that creates the object.

But it can be changed by user.

Example. Setting parent defeats constructor property

function Fcon() {}
const jj = new Fcon();
const pp = Reflect.getPrototypeOf(jj);

console.assert(Object.hasOwn(jj, "constructor") === false);
console.assert(Object.hasOwn(pp, "constructor"));
console.assert(pp.constructor === Fcon);

Example. Adding a constructor property defeats constructor property

// create a array
const aa = [];

// set a property key constructor
aa.constructor = RegExp;

console.log(aa.constructor);
// [Function: RegExp]

// show paren's constructor key value
console.log(Reflect.getPrototypeOf(aa).constructor);
// [Function: Array]

Property string key "constructor" of Standard objects

Follows the same rule.

[
 Object,
 Array,
 Date,
 Function,
 RegExp,
]
 .forEach((fcon) => {
  const xobj = Reflect.construct(fcon, []);
  console.assert(Object.hasOwn(xobj, "constructor") === false);
  console.assert(Object.hasOwn(Reflect.getPrototypeOf(xobj), "constructor"));
  console.assert(Reflect.getPrototypeOf(xobj).constructor === fcon);
 });

🟢 TIP: Do Not Use the Constructor Property

There's no reliable way to find a object's creator function. Because:

〔see What is Constructor?

JavaScript. Constructor, Class