英文:
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?
答案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
让我们举一个例子来说明内部发生的情况:
-
定义一个普通对象
const a = {},这是对const a = new Object()的语法糖。 -
new 运算符 将 a 的 [[prototype]] 指向
Object.prototype。a[[prototype]] = Object.prototype -
Object.prototype 有一个
[[prototype]],其值为null -
因此
a[[prototype][[prototype]]=>Object.prototype[[prototype]]=>null
英文:
Let's take an example to illustrate what happens internally:
-
Define an ordinary object a
const a = {}which is Sugar syntax forconst a = new Object() -
new operator points a[[prototype]] to
Object.prototype.a[[prototype]] = Object.prototype -
Object.prototype has a
[[prototype]]which value isnull -
So
a[[prototype][[prototype]]=>Object.prototype[[prototype]]=>null
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


![为什么[[prototype]]在JavaScript对象中有两个层级的__proto__?](https://i.stack.imgur.com/UeAb6.png)
评论