[[Prototype]] vs prototype (Peter van der Zee on JSMentors)

Here’s a great explanation (by Peter van der Zee) of the key differences between [[Prototype]] and prototype in JavaScript. It finally makes sense now!

Note: to understand this explanation it would help to know what prototype is/does and how it works.

So we have [[Prototype]] and prototype. You can see prototype as the parent object to which [[Prototype]] (so __proto__) refers to on instances of the constructor. Hah, I’m sure that’s not confusing. So let me give an example :)

var A = function(){};
var a = new A();
log(a instanceof A); // true
log(A.prototype); // object
log(a.prototype); // undefined
log(a.__proto__); // object, in browsers that support the mapping
log(a.__proto__=== A.prototype); // true.

“A” has a prototype property (A.prototype). If you add new properties to that object, they will automagically be available on “a”. “a” has a [[Prototype]] internal property. It refers to A.prototype. It is an essential part of the prototypal chain because it actually determines the next part of the chain. If you change a.[[Prototype]], changes to A.prototype will no longer be reflected on “a”. In fact, all not “own” properties of a will no longer be accessible and are replaced by a new set of properties.

via [JSMentors] Object creation.

One thought on “[[Prototype]] vs prototype (Peter van der Zee on JSMentors)

Comments are closed.