나는 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.create
를 사용할 수 있습니다, 단지'넣어 makeSound'를'Cat' 객체에 추가합니다. – Bergi