问题
I've been reading about prototype chain in JavaScript and came to two slightly different definitions. It is said that every object in JavaScript has a prototype and that prototype in turn has another prototype. The top prototype(Grand) may also have prototype and the chain can continue. Now the chain will stop at one last object. JavaScript the Good parts says the chain terminates at Object.prototype and MDN says null is the final link where the chain terminates.
Javascript: The Good Parts
Every object is linked to a prototype object from which it can inherit properties. All objects created from object literals are linked to
Object.prototype, an object that comes standard with JavaScript.
MDN
Each object has an internal link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. null, by definition, has no prototype, and acts as the final link in this prototype chain.
Questions:
- What is the end of prototype chain in javascript -- null or Object.prototype? Or are
nullandObject.prototypeone and the same thing? - Are objects, which are not created from object literals, not linked to
Object.prototype? - Say I have an object
var x = { len: 4, breadth: 5}. Would JavaScript automatically create it's prototypex.prototype. And how long would the prototype chain be? Wouldx.prototypehave only one prototypeObject.prototypemaking a 3 point chain?- How does JavaScript internally creates automatic prototypes?
回答1:
It is like, if New York has an envelop and inside, it says Colorado, and Colorado has an envelop, and inside, it says San Francisco, and San Francisco has an envelop, and inside, it says "none". So is San Francisco end of the chain, or is "none" the end of chain? It may depend on how you look at it. But one thing is for sure: it points up and up the chain, for inheritance purpose (prototypal inheritance), until it reaches
null, which means can't go further up. And make sure you know that, to go up and up the chain, it is__proto__. It is notprototype.Object.prototype,Animal.prototypeare different fromx.__proto__. The former are function objects (Object, Animal) having aprototypeproperty pointing to a prototype object. Andx.__proto__is how the prototype chain is followed upward. To go up and up, it isx.__proto__.__proto__and so on. See JavaScript's Pseudo Classical Inheritance diagram to understand it more.Object.prototyperefers to a prototype object. Quoted from MDN,null"represents the intentional absence of any object value. It is one of JavaScript's primitive values." SoObject.prototypeandnullare not the same thing.All JavaScript objects will have
obj.__proto__referring ultimately to whatObject.prototyperefers to. If it is notobj.__proto__, then it isobj.__proto__.__proto__. If not, just go up, and up, and it will reach the prototype object whichObject.prototyperefers to. And at this point, when you go up one level (by adding a.__proto__, then you getnull. You can try it in Google Chrome's developer's tool:x = { a : 1 } > Object {a: 1} x.__proto__ === Object.prototype > true x.__proto__.__proto__ > null Object.prototype.__proto__ > null
回答2:
What is the end of prototype chain in javascript
Null. The only authority on the language is ECMA-262.
Are objects, which are not created from object literals, not linked to Object.prototype
They may or many not be, e.g.
var x = Object.create(null)
has a [[Prototype]] of null, whereas:
var y = {};
has a [[Prototype]] of Object.prototype.
Say I have an object var x = { len: 4, breadth: 5}. Would javascript automatically create it's proptotype x.prototype.
No. Function objects have default prototype objects. Plain objects have a default [[Prototype]] (i.e. internal prototype property) that is Object.prototype (unless constructed as above).
And how long would the prototype chain be? Would x.prototype have only one prototype Object.prototype making a 3 point chain?
Two values: Object.prototype and null.
How does javascript internally creates automatic prototypes?
However it likes, the language specification does not define implementation, only behaviour.
回答3:
What is the end of prototype chain in javascript --
nullorObject.prototype? Or are null and Object.prototype one and the same thing?
null. Consider this code in a normal JavaScript environment:
var o = {};
console.log(o.i_am_a_property_that_does_not_exist);
That property accessor operation (o.i_am_a_property_that_does_not_exist) ends up going to the OrdinaryGet abstract operation defined by the specification with O set to the o object above and P set to "i_am_a_property_that_does_not_exist". That operation starts like this:
- Assert: IsPropertyKey(P) is true.
- Let desc be ? O.[GetOwnProperty].
If desc is undefined, then
a. Let parent be ? O.[GetPrototypeOf].
b. If parent is null, return undefined.
c. Return ? parent.[[Get]](P, Receiver).
...
For my example above, that [[Get]] operation in 3.c. ends up calling OrdinaryGet recursively until we run out of prototypes. As we can see, the chain ends when we reach null.
Moreover, it's entirely possible to have an object with a prototype chain that doesn't include Object.prototype at all (we'll see some in a moment). So clearly Object.prototype can't be the end of the prototype chain.
Are objects, which are not created from object literals, not linked to
Object.prototype?
The vast majority will be linked to Object.prototype directly or indirectly. Consider:
function Thing() {
}
var t = new Thing();
t's prototype is the object referenced by Thing.prototype. Thing.prototype's prototype is Object.prototype. So t is linked to Object.prototype (indirectly).
But it's entirely possible for an object not to be linked to Object.prototype. Here's one way:
var o = Object.create(null);
Object.create lets us create an object with the prototype we specify in the first argument (null in the above). So o above has no prototype, its [[Prototype]] internal slot (where objects remember their prototypes) is null.
Here's another:
function Thing() {
}
Thing.prototype = Object.create(null);
var t = new Thing();
In that case, although t has a prototype, its prototype's prototype is null; t isn't linked to Object.prototype at all.
Say I have an object
var x = { len: 4, breadth: 5}. Would JavaScript automatically create it's prototypex.prototype.
An object's prototype is not a property called prototype, so no, the JavaScript engine wouldn't create x.prototype. An object's prototype is linked via its [[Prototype]] internal slot, which is not directly observable but can be retrieved via Object.getPrototypeOf. (And if the object derives from Object.prototype, then on browsers only [in theory], you can use the Annex B [browser-only] __proto__ property from Object.prototype to access it. But not all objects have that property, because...not all objects inherit from Object.prototype!)
The prototype property is just used on functions to determine what object to use as the [[Prototype]] of new objects created via new with that function. Non-function objects don't have it, and if they did, it wouldn't be any more special than a property called foo or bazinga.
And how long would the prototype chain be? Would
x.prototypehave only one prototypeObject.prototypemaking a 3 point chain?
You're close, but again, the prototype property is not the prototype of the object, and non-function objects typically won't have a prototype property. For var x = { len: 4, breadth: 5}, the inheritance chain would be:
xx's [[Prototype]] (which isObject.prototype)Object.prototype's [[Prototype]], which isnull
So quite short; 1, 2, or 3 depending on whether you want to count x and whether you want to count null.
How does JavaScript internally creates automatic prototypes?
It doesn't, other than the ones defined by the spec (e.g., Object.prototype and such). The closest it comes is that for all function functions, the JavaScript engine automatically creates an object and assigns that object to the function's prototype property just in case that function is used as a constructor (via new). (It doesn't do this with arrow functions or generators.)
来源:https://stackoverflow.com/questions/36692927/what-is-the-end-of-prototype-chain-in-javascript-null-or-object-prototype