Why doesnt this javascript inheritance work? [closed]
Im trying to get my head around javascript inheritance and this code doesnt work cant see why:
function Animal(){
this.hasfur = true;
}
function Cat(){
this.sound = "M开发者_JS百科eow";
}
$(document).ready(function(){
Cat.protptype = new Animal();
var myCat = new Cat();
console.log(myCat.hasfur);
}
The console comes out with undefined. But I thought that when I access myCat.hasfur it should look at the prototype of cat which is vehicle and then look at that property...?
Read that again:
Cat.protptype
I'm pretty sure you meant:
Cat.prototype
It is just a typo at
Cat.protptype = new Animal();
write
Cat.prototype = new Animal();
instead.
精彩评论