javascript中的原型链的末尾是什么?null或Object.prototype? [英] What is the end of prototype chain in javascript -- null or Object.prototype?

查看:352
本文介绍了javascript中的原型链的末尾是什么?null或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?或者nullObject.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 and Object.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 prototype x.prototype. And how long would the prototype chain be? Would x.prototype have only one prototype Object.prototype making a 3 point chain?
      • How does JavaScript internally creates automatic prototypes?

      推荐答案

      1. 就像,如果纽约有一个信封,里面说科罗拉多,科罗拉多有一个信封,里面说旧金山,而旧金山有信封,里面说没有".那么,旧金山是链的尽头,还是没有"链的尽头?这可能取决于您如何看待它.但是有一点是肯定的:出于继承(原型继承)的目的,它向上和向上指向链,直到达到null,这意味着无法进一步发展.并确保您知道,要沿链向上移动,它是__proto__.不是prototype.

      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 not prototype.

      Object.prototypeAnimal.prototypex.__proto__不同.前者是具有prototype属性的函数对象(对象,动物),该属性指向原型对象. x.__proto__是原型链如何向上跟踪的方式.要上升和上升,它是x.__proto__.__proto__,依此类推.请参阅 JavaScript的伪古典继承图以了解它.更多.

      Object.prototype, Animal.prototype are different from x.__proto__. The former are function objects (Object, Animal) having a prototype property pointing to a prototype object. And x.__proto__ is how the prototype chain is followed upward. To go up and up, it is x.__proto__.__proto__ and so on. See JavaScript's Pseudo Classical Inheritance diagram to understand it more.

      Object.prototype是指原型对象. 引自MDN null代表故意没有任何对象值.这是JavaScript的原始值之一."所以Object.prototypenull不是同一回事.

      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." So Object.prototype and null 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 what Object.prototype refers to. If it is not obj.__proto__, then it is obj.__proto__.__proto__. If not, just go up, and up, and it will reach the prototype object which Object.prototype refers to. And at this point, when you go up one level (by adding a .__proto__, then you get null. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆