Object.prototype.constructor

La proprietà constuctor restituisce un riferimento alla funzione del costruttore Object che ha creato l'oggetto istanza. Notare che il valore di questa proprietà è un riferimento alla funzione stessa, non una stringa contenente il nome della funzione.Il valore è di sola lettura solo per i valori primitivi come 1, true e "test".

Description

Tutti gli oggetti (ad eccezione degli object.create (null)) avranno una proprietà constuctor. Gli oggetti creati senza l'uso esplicito di una funzione di constructor (come oggetti letterali e array-letterali) avranno una proprietà constructor che punta al tipo di costrunctor dell'oggetto fondamentale per quell'oggetto.
let o = {}
o.constructor === Object // true

let o = new Object
o.constructor === Object // true

let a = []
a.constructor === Array // true

let a = new Array
a.constructor === Array // true

let n = new Number(3)
n.constructor === Number // true

Examples

Displaying the constructor of an object

The following example creates a constructor (Tree) and an object of that type (theTree). The example then displays the constructor property for the object theTree.

function Tree(name) {
  this.name = name
}

let theTree = new Tree('Redwood')
console.log('theTree.constructor is ' + theTree.constructor)

This example displays the following output:

theTree.constructor is function Tree(name) {
  this.name = name
}

Changing the constructor of an object

One can assign the constructor property for any value except null and undefined since those don't have a corresponding constructor function (like String, Number, Boolean etc.), but values which are primitives won't keep the change (with no exception thrown). This is due to the same mechanism, which allows one to set any property on primitive values (except null and undefined) with no effect. namely wherenever one uses such a primitive as an object an instance of the corresponding constructor is created and discarded right after the statement was executed.

let val = null;
val.constructor = 1; //TypeError: var is null

val = 'abc';
val.constructor = Number; //val.constructor === String

val.foo = 'bar'; //An implicit instance of String('abc') was created and assigned the prop foo
val.foo === undefined; //true, since a new instance of String('abc') was created for this comparison, which doesn't have the foo property

So basically one can change the value of the constructor property for anything, except the primitives mentioned above, note that changing the constructor property does not affect the instanceof operator:

let a = [];
a.constructor = String
a.constructor === String // true
a instanceof String //false
a instanceof Array //true

a = new Foo();
a.constructor = 'bar'
a.constructor === 'bar' // true

//etc.

If the object is sealed/frozen then the change has no effect and no exception is thrown:

let a = Object.seal({});
a.constructor = Number;
a.constructor === Object; //true

Changing the constructor of a function

Mostly this property is used for defining a function as a function-constructor with further calling it with new and prototype-inherits chain.

function Parent() { /* ... */ }
Parent.prototype.parentMethod = function parentMethod() {}

function Child() {
   Parent.call(this) // Make sure everything is initialized properly
}
Child.prototype = Object.create(Parent.prototype) // re-define child prototype to Parent prototype

Child.prototype.constructor = Child // return original constructor to Child

But when do we need to perform the last line here? Unfortunately, the answer is: it depends.

Let's try to define the cases in which re-assignment of the original constructor will play a major role, and when it will be one superfluous line of code.

Take the following case: the object has the create() method to create itself.

function Parent() { /* ... */ }
function CreatedConstructor() {
   Parent.call(this)
}

CreatedConstructor.prototype = Object.create(Parent.prototype)

CreatedConstructor.prototype.create = function create() {
  return new this.constructor()
}

new CreatedConstructor().create().create() // TypeError undefined is not a function since constructor === Parent

In the example above the exception will be shown since the constructor links to Parent.

To avoid this, just assign the necessary constructor you are going to use.

function Parent() { /* ... */ }
function CreatedConstructor() { /* ... */ }

CreatedConstructor.prototype = Object.create(Parent.prototype)
CreatedConstructor.prototype.constructor = CreatedConstructor // sets the correct constructor for future use

CreatedConstructor.prototype.create = function create() {
  return new this.constructor()
}

new CreatedConstructor().create().create() // it's pretty fine

Ok, now it's pretty clear why changing the constructor can be useful.

Let's consider one more case.

function ParentWithStatic() {}

ParentWithStatic.startPosition = { x: 0, y:0 } // Static member property
ParentWithStatic.getStartPosition = function getStartPosition() {
  return this.startPosition
}

function Child(x, y) {
  this.position = {
    x: x,
    y: y
  }
}

Child = Object.assign(ParentWithStatic)
Child.prototype = Object.create(ParentWithStatic.prototype)
Child.prototype.constructor = Child

Child.prototype.getOffsetByInitialPosition = function getOffsetByInitialPosition() {
  let position = this.position
  let startPosition = this.constructor.getStartPosition() // error undefined is not a function, since the constructor is Child

  return {
    offsetX: startPosition.x - position.x,
    offsetY: startPosition.y - position.y
  }
};

For this example we need either to stay parent constructor to continue to work properly or reassign static properties to child's constructor:

...
Child = Object.assign(ParentWithStatic) // Notice that we assign it before we create(...) a prototype below
Child.prototype = Object.create(ParentWithStatic.prototype)
...

or assign parent constructor identifier to a separate property on the Child constructor function and access it via that property:

...
Child.parentConstructor = ParentWithStatic
Child.prototype = Object.create(ParentWithStatic.prototype)
...
   let startPosition = this.constructor.parentConstructor.getStartPosition()
...

Summary: Manually updating or setting the constructor can lead to differrent and sometimes confusing consequences. To prevent this, just define the role of constructor in each specific case. In most cases, constructor is not used and reassignment of it is not necessary.

Specifications

Browser compatibility

BCD tables only load in the browser

See also