为什么[[prototype]]在JavaScript对象中有两个层级的__proto__?

huangapple go评论53阅读模式
英文:

Why does [[prototype]] has two level of __proto__ in javascript objects?

问题

当我们创建一个对象并查看它的 [[prototype]] 时,有一个 __proto__ 属性,进一步扩展后,还有另一个 __proto__,但其值为 null。

这两个级别的 __proto__ 的原因是什么?为什么在第二次出现时它的值为 null?

英文:

When we create an object and look at the [[prototype]] of it, there is a __proto__ property and even further we expand there is the same set of properties with another __proto__ but with the null value.

What is the reason for these two levels of __proto__ and why it is null in the second occurrence?

为什么[[prototype]]在JavaScript对象中有两个层级的__proto__?

答案1

得分: 1

Wow, that's really confusing. __proto__ is a (deprecated) getter/setter, and expanding it in the console will run the getter on the start of the prototype chain. It then returns Object.protype, and expanding __proto__ on that result object will now result in null.

But my advice is to completely ignore __proto__. It is deprecated and should not be used anywhere, in the console only look at the [[prototype]] which displays the prototype chain links.

英文:

Wow, that's really confusing. __proto__ is a (deprecated) getter/setter, and expanding it in the console will run the getter on the start of the prototype chain. It then returns Object.protype, and expanding __proto__ on that result object will now result in null.

But my advice is to completely ignore __proto__. It is deprecated and should not be used anywhere, in the console only look at the [[prototype]] which displays the prototype chain links.

答案2

得分: 0

让我们举一个例子来说明内部发生的情况:

  1. 定义一个普通对象 const a = {},这是对 const a = new Object() 的语法糖。

  2. new 运算符 将 a 的 [[prototype]] 指向 Object.prototypea[[prototype]] = Object.prototype

  3. Object.prototype 有一个 [[prototype]],其值为 null

  4. 因此 a[[prototype][[prototype]] => Object.prototype[[prototype]] => null

英文:

Let's take an example to illustrate what happens internally:

  1. Define an ordinary object a const a = {} which is Sugar syntax for const a = new Object()

  2. new operator points a[[prototype]] to Object.prototype. a[[prototype]] = Object.prototype

  3. Object.prototype has a [[prototype]] which value is null

  4. So a[[prototype][[prototype]] => Object.prototype[[prototype]] => null

huangapple
  • 本文由 发表于 2023年6月5日 08:43:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定