我想知道为什么基于我的代码,cat.getIt();不会工作。我创建了两个对象,并希望" cat"反对拥有"狗"对象作为其原型。它起作用但是没有用的是狗方法" getIt"即使我说过:Cat.prototype = new Dog()(Cat.prototype = Object.create(Dog.prototype);也没有工作),它不会被对象cat继承。有谁知道为什么?
function Dog (name){
this.name=name;
this.age=[];
}
var myDog= new Dog("Jo");
Dog.prototype.sayHi=function(){
return "My name is " + this.name + ", I'm a Dog";
};
Dog.prototype.getIt = function(){
var small = {
name: "Baby " + this.name
};
this.age = [small];
return small;
}
myDog.getIt();
function Cat(name,color){
Dog.call(this,name);
this.color=color;
}
var cat = new Cat("Lexy", "red");
//Cat.prototype=Object.create(Dog.prototype); //Or should I do it like that?
Cat.prototype=new Dog();
cat.getIt();