Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

ecma262 - Relation between [[Prototype]] and prototype in JavaScript

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 can get the [[Prototype]] property using Object.getPrototypeOf(<obj>). (This method is specified in ECMAScript 5. Older versions of JavaScript does not have any standard way of reading [[Prototype]]).

You can usually get to the prototype through the constructor, e.g.:

obj.constructor.prototype == Object.getPrototypeOf(obj) 

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 != Object.getPrototypeOf(obj)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...