I don't understand the difference between Object and Object.constructor.
Let's take an example :
function Person() {}
var p = new Person();
Person is the function to create objects. So :
p instanceof Person //true
p.constructor // Person
Person is the constructor and I can create persons with new Person()
But Object is also a constructor ( I can create object with new Object()). So why Object has a constructor property since it's already a constructor itself ?
Thanks
2 Answers 2
So why does it have a constructor property since it's already a constructor itself?
Because every constructor is a function, and those inherit their properties (like the call, apply and bind methods) from Function.prototype. Including the Function.prototype.constructor property, which points to Function.
Comments
constructor is a pointer towards the constructor function used to create a given value, all objects have one. For primitives it points towards the primitive constructor: Object, Number, Function and so on.
Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
Person.constructoris inherited fromFunction.prototypeas it is for all (constructor or not) functions