Javascript中_proto__的属性

Javascript中_proto__的属性,javascript,Javascript,我有下面的代码 var person={ firstname:"Geet", getFullName:function(){ return this.firstname; } } var test={ firstname:"Test" } test.__proto__=person; for (var prop in test){ console.log(prop + ': '+test[prop]); } 输出:- firstna

我有下面的代码

var person={
    firstname:"Geet",
    getFullName:function(){
        return this.firstname;
    }
}

var test={
    firstname:"Test"
}

test.__proto__=person;
for (var prop in test){
    console.log(prop + ': '+test[prop]);
}
输出:-

 firstname: Test
 getFullName: function (){
     return this.firstname;
 }

链条是怎么停在那里的。为什么它不打印person对象的proto属性。这是如何处理的。

您根本不应该使用
\uuuu proto\uuuu
,请参阅

for..in仅迭代对象及其
[[Prototype]]
链的可枚举属性。所以你们可以直接在测试中得到名字。然后是在person上的getFullName

您无法获取person.firstname,因为它在测试时被firstname属性“shaddowed”。也就是说,
[[Prototype]]
链上的其余对象不再具有可枚举属性,即Object.Prototype和null


如果您希望看到
\uuuu proto\uuuu
属性,则无法看到,因为它不可枚举,请参见。

(测试中的var prop){
首先不会像那样迭代原型。
person
的原型是
Object.prototype
。该对象没有任何可枚举属性。更简单的示例:迭代
test={}
不会记录任何内容。