views:

962

answers:

3

From http://www.jibbering.com/faq/faq_notes/closures.html :

Note: ECMAScript defines an internal [[prototype]] property of the internal Object type. This property is not directly accessible with scripts, but it is the chain of objects referred to with the internal [[prototype]] property that is used in property accessor resolution; the object's prototype chain. A public prototype property exists to allow the assignment, definition and manipulation of prototypes in association with the internal [[prototype]] property. The details of the relationship between to two are described in ECMA 262 (3rd edition) and are beyond the scope of this discussion.

What are the details of the relationship between the two? I've browsed through ECMA 262 and all I've read there is stuff like:

The constructor’s associated prototype can be referenced by the program expression constructor.prototype,

Native ECMAScript objects have an internal property called [[Prototype]]. The value of this property is either null or an object and is used for implementing inheritance.

Every built-in function and every built-in constructor has the Function prototype object, which is the initial value of the expression Function.prototype

Every built-in prototype object has the Object prototype object, which is the initial value of the expression Object.prototype (15.3.2.1), as the value of its internal [[Prototype]] property, except the Object prototype object itself.

From this all I gather is that the [[Prototype]] property is equivalent to the prototype property for pretty much any object. Am I mistaken?

+18  A: 

I believe you are right in most cases.

Every object has a hidden [[Prototype]] property, which is used for inheritance. Functions additionally have a public prototype property, which is used only when the function is used as constructor: When an object is constructed using new, the [[Prototype]] property of the new object is set to the prototype property of the function that was used as constructor.

E.g.

function C() {}
C.prototype = P1;  
var obj = new C();  // obj.[[Prototype]] is now P1.

You cannot actually read the [[Prototype]] property of an object since it is hidden. You can usually get to the prototype through the constructor, eg.:

obj.constructor.prototype == obj.[[Prototype]] 

But this is not always the case, since the prototype property of the constructor function can be reassigned, but the [[Prototype]] of an object cannot be reassigned after the object is created. So if you do:

C.prototype = P2;

then

obj.constructor.prototype != obj.[[Prototype]]

Because the prototype of C is now P2, but [[Prototype]] of obj is still P1.

Note that it is only functions that have a prototype property. Note also that the prototype property of a function is not the same as the [[Prototype]] property of the function!

JacquesB
exactly what i was looking for. thanks for the very clear answer! i had 'figured' all of these, but hadn't fleshed it out so exactly.
Claudiu
@olavk - That was a great answer and helped me understand inheritance although it does say on the mozilla site that the __proto__ property can be changed to point to a different object after initial construction. https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/proto
Nick Lowman
something to add: i believe in Firefox you can access `[[Prototype]]` as `__proto__`
Claudiu
+3  A: 

In addition to olavk's answer: Some JavaScript implementations (eg mozilla's) allow to access the [[Prototype]] property directly...

Christoph
Nice,Christoph, and welcome to SO!
some
Thanks. Just trying to help where I can ;)
Christoph
+9  A: 

To answer your question directly: logically it is an object's private copy of the prototype property of its constructor. Using metalanguage this is how objects are created:

// not real JS

var Ctr = function(...){...};
Ctr.prototype = {...}; // some object with methods and properties

// the object creation sequence: var x = new Ctr(a, b, c);
var x = {};
x["[[prototype]]"] = Ctr.prototype;
var result = Ctr.call(x, a, b, c);
if(typeof result == "object"){ x = result; }
// our x is fully constructed and initialized at this point

At this point we can modify the prototype, and the change will be reflected by all objects of the class, because they refer to the prototype by reference:

Ctr.prototype.log = function(){ console.log("...logging..."); };

x.log();  // ...logging..

But if we change the prototype on the constructor, already created objects will continue referring to the old object:

Ctr.prototype = {life: 42};
// let's assume that the old prototype didn't define "life"

console.log(x.life);  // undefined
x.log();              // ...logging...

In the full accordance with the standard [[prototype]] is not available, but Mozilla extends the standard with __proto__ property (read-only), which is exposing the normally hidden [[prototype]]:

Again, __proto__ can be legalized in the next ES3.1 standard.

Eugene Lazutkin
Hmm.. instead of standardizing __proto__, they created Object.getPrototypeOf(target), which will return target.[[Prototype]]. The main difference is that you can't assign to the result.
Sean McMillan