为什么这个不起作用?
var sheep = function(options){
this.options = {sizes: 100,
eat: 100,
colors: 'white',
running: function () {
return this.sizes + this.eat;
}
}
};
var blacksheep = new sheep({colors:'black'});
alert('blackcsheep color is ' + blacksheep.colors);//error undefined
alert('blackcsheep color is ' + blacksheep.options.colors);// it return white
alert('blackcsheep running is ' + blacksheep.running());//error
sheep
已经是一个对象了。我认为阅读有关JavaScript基础知识的内容会对您最有帮助:MDN JavaScript指南,特别是使用对象。 - Felix Klingsheep
的副本,这两个变量只是引用同一个对象。这与原型继承无关。 - Felix Kling