我很奇怪,爲什麼在這段代碼,當我試圖訪問該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