【发布时间】:2014-05-04 12:04:26
【问题描述】:
我正在尝试通过使用 Chrome 开发者控制台来了解原型继承。这是我用来设置原型链的代码:
var Organism = Object.create(null);
undefined
var Animal = Object.create(Organism);
undefined
var Mammal = Object.create(Animal);
undefined
var Dog = Object.create(Mammal);
undefined
var Spot = Object.create(Dog);
undefined
我可以为有机体和哺乳动物对象添加一些属性:
Mammal.hasHair = true;
true
Organism.hasHair = false;
false
接下来我为 Dog 对象定义一些变量:
Dog.numLegs = 4;
4
Dog.speak = function(){return 'woof, woof!';};
function (){return 'woof, woof!';}
最后我为 Spot 定义了一些变量
Spot.color = 'White';
"White"
Spot.pattern = 'Spots';
"Spots"
Spot.patternColor = 'Black';
"Black"
Spot.weight = 22
22
但是由于某种原因,当我检查 Dog 对象时,它会输出这样的属性,但我看不到它的原型...
Spot;
Object {color: "White", pattern: "Spots", patternColor: "Black", weight: 22}
或者如果我尝试这个我仍然看不到它的原型......
dir(Spot);
Object
color: "White"
pattern: "Spots"
patternColor: "Black"
weight: 22
如果我没记错的话,我应该没有一个名为_proto_ 的属性,它指向Dog Object 的原型,即哺乳动物?如何检查对象以查看此属性?
【问题讨论】:
-
顺便说一句,它是
__proto__(每边两个下划线),而不是_proto_。
标签: javascript google-chrome prototypal-inheritance