<aside> 💡 Study Note 19장 프로토타입

</aside>

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0aa805b3-ce4c-4cba-adc8-da95bbf6c4de/스크린샷_2021-07-22_오후_6.57.52.png

오버라이딩과 프로퍼티 섀도잉

오버라이딩(Overriding)이란?

: 부모 클래스의 기능을 재정의 하는 것

⇒ 부모 클래스의 기능을 사용하지 않고 자식 클래스에서 구현한 기능을 사용하고 싶은 경우

⇒ 부모 클래스의 기능을 자식 클래스에서 확장하고 싶은 경우 사용

function Person(name) {
    this.name = name;
}

Person.prototype.sayHello = function() {
    console.log(`HI! My name is ${this.name}`);
};

const person1 = new Person('sujin');

person1.sayHello();   // HI! My name is sujin

⇒ person1는 Person의 자식이라고 볼 수 있음

⇒ Person.prototype은 쉽게 말하면 부모의 유전자, 내포하고 있는 기능

⇒ 자식이기 때문에 부모인 Person의 유전자에 접근 가능

person1.sayHello = function() {
    console.log(`HELLO! I'm ${this.name}`);
}

person1.sayHello();   // HELLO! I'm sujin

⇒ Person.prototype의 sayHello 메서드를 덮어 쓴 것이 아니라

새로운 person1 객체의 sayHello 메서드로 추가된 것

⇒ 상속관계에 의해 프로퍼티(메서드)가 가려지는 현상을 프로퍼티 섀도잉

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/86d5ab58-5adb-4bc7-8dc7-bb778ccbd382/IMG_0734.jpg