简体   繁体   English

JavaScript 原型链参考 Object.prototype

[英]JavaScript Prototype Chain Reference to Object.prototype

I need some help for understanding the prototype chain.我需要一些帮助来理解原型链。 I don't understand / not quite sure if my assumptions are correct.我不明白/不太确定我的假设是否正确。 I want to understand the link to the Object.prototype.我想了解 Object.prototype 的链接。 One example一个例子

function A(){};
var newObject=new A();

I know the newObject has an internal property [[prototype]] which has a reference to A.prototype.我知道 newObject 有一个内部属性 [[prototype]],它引用了 A.prototype。 And this prototype-Object has also an internal property with reference to Object.prototype, right?而且这个prototype-Object也有一个内部属性参照Object.prototype,对吧? But why does it have that reference?但为什么它有那个参考? Is it because the prototype of A (used as constructor) is an object which can be imagined to be created by A.prototype=new Object() ( which will be done automatically in the background).是不是因为 A 的原型(用作构造函数)是 object 可以想象由 A.prototype=new Object() 创建(将在后台自动完成)。 And now I have a reference to the prototype of Object?现在我有参考Object的原型? I hope I explained it clearly.我希望我解释清楚。 Please let me know your comments.请让我知道您的意见。

Many thanks非常感谢

Yes, your understanding is correct.是的,你的理解是正确的。

In JS, pretty much all the objects have a reference to Object.prototype in their prototype chain.在 JS 中,几乎所有对象的原型链中都有对Object.prototype的引用。

The [[Prototype]] of an object refers to Object.prototype as the end of a prototype chain. object 的[[Prototype]]指的是Object.prototype作为原型链的末端。 In your example, this means what you described:在您的示例中,这意味着您所描述的内容:

            [[Prototype]]                  [[Prototype]]                   
newObject -----------------> A.prototype -----------------> Object.prototype

That applies to...这适用于...

  • objects created by the Object constructor (ie new Object(...) ),Object构造函数创建的对象(即new Object(...) ),
  • those created with an object literal (which you can think of as syntax sugar for a new Object() call followed by a bunch of Object.defineProperty calls) and使用 object 文字创建的那些(您可以将其视为new Object()调用的语法糖,然后是一堆Object.defineProperty调用)和
  • objects created internally by the JS interpreter (like A.prototype which is created automatically when you define the A function).由 JS 解释器内部创建的对象(如定义A函数时自动创建的A.prototype )。

There are also exceptions of this "rule" though:不过,这个“规则”也有例外:

  • Object.prototype itself. Object.prototype本身。

    If it had a reference to... well, itself, it would create and infinite recursion when a nonexistent property is looked up.如果它有对...的引用,那么,它本身就会在查找不存在的属性时创建无限递归。 Hence, it has no prototype , which means that its [[Prototype]] is null (that's how a prototype chain "ends").因此,它没有原型,这意味着它的[[Prototype]]null (这就是原型链“结束”的方式)。

  • Custom objects created using Object.create or modified with Object.setPrototype .使用Object.setPrototype创建或使用Object.create修改的自定义对象。

    The [[Prototype]] can be set to anything this way, including null or other objects whose prototype chain doesn't end with Object.prototype . [[Prototype]]可以通过这种方式设置为任何内容,包括null或其他原型链不以Object.prototype结尾的对象。

This is indeed what the ECMAScript specification prescribes.这确实是 ECMAScript 规范所规定的。 When a function is defined with the function keyword, then an object is created from Object.prototype which is assigned to the function's prototype property, and therefor will be used as prototype for any object that is constructed by that function. When a function is defined with the function keyword, then an object is created from Object.prototype which is assigned to the function's prototype property, and therefor will be used as prototype for any object that is constructed by that function.

This specification can be found at Make Constructor , step 5.a:此规范可在Make Constructor的步骤 5.a 中找到:

5.a. 5.a. Set prototype to OrdinaryObjectCreate(%Object.prototype%).原型设置为 OrdinaryObjectCreate(%Object.prototype%)。

When it comes to javascript every value is either an object (arrays, objects, sets, maps...) or a primitive(Numbers, strings, bool...) objects and primitives are the two data types in javascript a.当涉及到 javascript 时,每个值要么是 object(数组、对象、集合、映射...),要么是基元(数字、字符串、布尔...)对象和基元是 ZDE9B9ED78D7E2E1DCEEFFEE780E2F91 中的两种数据类型。 You have the Number, String, Array and even Object constructors which you can use to create the corresponding value.您可以使用 Number、String、Array 甚至 Object 构造函数来创建相应的值。 And when you create new object using the new operator the following steps happen consecutively:当您使用 new 运算符创建新的 object 时,以下步骤会连续发生:

// 1. New empty object is created // 1. 创建新的空 object

// 2. the constructor function is called, and the this keyword is assigned to the empty object ie this = {}. // 2.构造函数function被调用,this关键字赋值给空的object即this = {}。

// 3. the empty object is linked to the prototype of the constructor functio ie {}. // 3.空的object链接到构造函数的原型即{}。 proto = constructorFunction.prototype. proto = constructorFunction.prototype。

// 4. the constructor function automatically return the new object {}. // 4.构造函数function自动返回新的object {}。

keep in mind that constructorFunction.prototype is the prototype of the new object that is created from the constructorFunction and not of itself.请记住,constructorFunction.prototype 是新 object 的原型,它是从 constructorFunction 而不是自身创建的。

I hope this clears things up a little for you!我希望这可以为您解决一些问题!

But why does it have that reference?但为什么它有那个参考?

To answer in one sentence - because prototype is an object itself and has its own prototype.一句话回答——因为prototype本身就是一个object,有自己的prototype。 When you create function in JavaScript it has a property called prototype.当您在 JavaScript 中创建 function 时,它具有称为原型的属性。 When you use that function as a constructor function (with new operator), new object is creating which as you wrote is linked to its constructor function prototype via internal [[prorotype]] property. When you use that function as a constructor function (with new operator), new object is creating which as you wrote is linked to its constructor function prototype via internal [[prorotype]] property. That prototype object is plain JavaScript object which [[prototype]] property references Object.prototype.该原型 object 是普通的 JavaScript object 其中 [[原型]] 属性引用 Z497031794414A552435F901。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM