javascript中的原型链的末尾是什么?null或Object.prototype? [英] What is the end of prototype chain in javascript -- null or Object.prototype?
问题描述
我一直在阅读有关JavaScript的原型链的知识,并得出了两个略有不同的定义.据说JavaScript中的每个对象都有一个原型,而该原型又具有另一个原型.顶级原型(Grand)也可能具有原型,并且链可以继续.现在,链条将停在最后一个对象处. JavaScript的好部分说,链终止于Object.prototype
,而 MDN 说null
是链终止的最终链接.
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:好的部分
Javascript: The Good Parts
每个对象都链接到一个原型对象,可以从该对象继承属性. 所有从对象文字创建的对象都链接到Object.prototype
,这是JavaScript的标准对象.
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
MDN
每个对象都有一个内部链接,该链接指向另一个称为其原型的对象.该原型对象具有自己的原型,依此类推,直到到达以null作为其原型的对象为止.按定义,null没有原型,并充当该原型链的最终链接..
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.
问题:
- javascript中的原型链的末尾是什么?null或Object.prototype?或者
null
和Object.prototype
是一回事吗? - 不是不是由对象文字创建的,未链接到
Object.prototype
的对象吗? - 说我有一个对象
var x = { len: 4, breadth: 5}
. JavaScript是否会自动创建其原型x.prototype
.原型链将持续多久?x.prototype
只有一个原型Object.prototype
会构成3点链吗?- JavaScript如何在内部创建自动原型?
- What is the end of prototype chain in javascript -- null or Object.prototype? Or are
null
andObject.prototype
one 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.prototype
have only one prototypeObject.prototype
making a 3 point chain?- How does JavaScript internally creates automatic prototypes?
推荐答案
-
就像,如果纽约有一个信封,里面说科罗拉多,科罗拉多有一个信封,里面说旧金山,而旧金山有信封,里面说没有".那么,旧金山是链的尽头,还是没有"链的尽头?这可能取决于您如何看待它.但是有一点是肯定的:出于继承(原型继承)的目的,它向上和向上指向链,直到达到
null
,这意味着无法进一步发展.并确保您知道,要沿链向上移动,它是__proto__
.不是prototype
.
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.prototype
与x.__proto__
不同.前者是具有prototype
属性的函数对象(对象,动物),该属性指向原型对象.x.__proto__
是原型链如何向上跟踪的方式.要上升和上升,它是x.__proto__.__proto__
,依此类推.请参阅 JavaScript的伪古典继承图以了解它.更多.Object.prototype
,Animal.prototype
are different fromx.__proto__
. The former are function objects (Object, Animal) having aprototype
property 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.prototype
是指原型对象. 引自MDN ,null
代表故意没有任何对象值.这是JavaScript的原始值之一."所以Object.prototype
和null
不是同一回事.Object.prototype
refers 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.prototype
andnull
are not the same thing.所有JavaScript对象的
obj.__proto__
最终都将引用Object.prototype
所引用的内容.如果不是obj.__proto__
,则为obj.__proto__.__proto__
.如果不是,则向上然后向上,它将到达Object.prototype
所引用的原型对象.至此,当您上升一级(通过添加.__proto__
时,您会得到null
.您可以在Google Chrome浏览器的开发人员工具中进行尝试:All JavaScript objects will have
obj.__proto__
referring ultimately to whatObject.prototype
refers 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.prototype
refers 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
这篇关于javascript中的原型链的末尾是什么?null或Object.prototype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!