【发布时间】:2010-10-13 15:21:44
【问题描述】:
function Gadget(name, color)
{
this.name = name;
this.color = color;
}
Gadget.prototype.rating = 3
var newtoy = new Gadget("webcam", "black")
newtoy.constructor.prototype.constructor.prototype.constructor.prototype
它总是返回 rating = 3 的对象。
但如果我执行以下操作:
newtoy.__proto__.__proto__.__proto__
链最终返回null。
同样在 Internet Explorer 中,如果没有 __proto__ 属性,我将如何检查 null?
【问题讨论】:
-
这个graph diagram 将帮助您了解prototype 和proto 之间的区别。你可以跟随 newtoy 对象的 proto 链,然后你就会明白为什么 newtoy 的第三个 Proto 是空的。
-
从图中也可以清楚地看出
newtoy.prototype不等于newtoy.constructor.prototype,因此newtoy.constructor.prototype将没有名为rating的属性。同样newtoy.constructor.prototype.constructor.property也不会有名为rating的属性。 -
最后评论中的错字:因此
newtoy.constructor.prototype将具有名为 rating 的属性。同样,newtoy.constructor.prototype.constructor.property也将具有称为 rating 的属性。
标签: javascript inheritance prototype-programming