我很奇怪,为什么在这段代码,当我试图访问该garfield
制成的,在这种情况下Cat
对象的属性,我得到undefined
:JS - 原型继承与对象创建功能
function Cat(){
this.legs = 2;
this.species = 'cat';
};
Cat.prototype.makeSound = function() {
console.log(this.sound); // logs undefined
};
const garfield = Object.create(Cat);
garfield.sound = 'feed me';
garfield.makeSound();
console.log(garfield.legs) // logs undefined
我不应该能够让原型继承链访问这些属性吗?
我没有看到一个理由使用'Object.setPrototypeOf'这里,只要把' 'Cat'对象上的makeSound'。 – Bergi