James Franco
James Franco

Reputation: 4706

Clarification on constructor and __proto__ property

I have been reading on the constructor and __proto__ in javascript.I think i understand the basics of the __proto__ property however I am having a little trouble with the constrcutor property. Here is what I understand so far regarding both properties please correct me if I am wrong or add if I missed some point. Every object in javascript has a "hidden" property called __proto__ which by default points to Object.prototype.Now when an instance of an object is created using new.The value of this property is modified and is assigned the object ConstructorName.prototype. I am not sure what the default value of __proto__ is before new is called.Now regarding .constructor I am a little confused I read this and so far believe that constructor property by default is empty and when new is invoked the constrcutor is modified and assigned ConstructorName.prototype.constructor. Kindly let me know if I am headed in the right direction .

Upvotes: 0

Views: 37

Answers (1)

Bergi
Bergi

Reputation: 664434

Every object in javascript has a "hidden" property called __proto__

No. The hidden property is called [[prototype]]. .__proto__ is a deprecated getter that accesses it, you should use Object.getPrototypeOf

which by default points to Object.prototype.

"default" might be misleading - it depends on how the object is created. For plain object literals, yes, it's Object.prototype.

Now when an instance of an object is created using new. The value of this property is modified and is assigned the object ConstructorName.prototype. I am not sure what the default value of __proto__ is before new is called.

There is no value before that. The object is created with the prototype being set from the very beginning. Before new is called, there is no object.

Now regarding .constructor I am a little confused I read this and so far believe that constructor property by default is empty and when new is invoked the constrcutor is modified and assigned ConstructorName.prototype.constructor.

No, nothing is assigned anywhere, there is no .constructor property on instances. They do inherit the property (along with others) from the .prototype object. The ConstructorName.prototype.constructor property is created right in the instance where the ConstructorName function is created.

Upvotes: 1

Related Questions