为什么javascript继承不起作用?
我试图了解javascript继承,但这段代码不起作用,我不明白为什么:为什么javascript继承不起作用?,javascript,Javascript,我试图了解javascript继承,但这段代码不起作用,我不明白为什么: function Animal(){ this.hasfur = true; } function Cat(){ this.sound = "Meow"; } $(document).ready(function(){ Cat.protptype = new Animal(); var myCat = new Cat(); console.log(myCat.hasfur); }
function Animal(){
this.hasfur = true;
}
function Cat(){
this.sound = "Meow";
}
$(document).ready(function(){
Cat.protptype = new Animal();
var myCat = new Cat();
console.log(myCat.hasfur);
}
控制台带有未定义的。但我认为,当我访问myCat.hasfur时,它应该查看作为车辆的cat原型,然后查看该属性…?再次阅读:
Cat.protptype
我很确定你的意思是:
Cat.prototype
这只是一个打字错误
Cat.protptype = new Animal();
写
相反。这可能很明显,但它不是
Cat.prototype
不是Cat.protptype
?抱歉,是的,只是看到没有运行时错误,所以没有发现它…抱歉,伙计们试图关闭问题是的,它是一个类型,因为它不是错误,只是语义错误抱歉!
Cat.prototype = new Animal();