<레퍼런스>
생활코딩 - JavaScript 객체 지향 프로그래밍 - 15. prototype vs proto)
(https://www.youtube.com/watch?v=wT1Bl5uV27Y)
Person 클래스를 정의할 때, 클래스 내부에는 prototype 프로퍼티가 생긴다.
Person.prototype은 Person 클래스의 원형 객체를 의미한다. 아래 코드는 Person의 프로토타입 객체에 sum이라는 메소드를 추가하는 것이다.
Person.prototype.sum = function(){}
Person의 프로토타입 객체에 있는 constructor(생성자) 함수는 Person 클래스를 가리킨다.
Person 클래스를 이용해 kim이라는 인스턴스를 생성한다고 가정하자. 이떄 kim 인스턴스 내부에는 __proto__라는 프로퍼티가 생성되는데, 이는 즉 kim을 생성한 Person 클래스의 원형 객체(프로토타입)을 가리킨다.
class Idol { constructor(name, age, position) { this.name = name; this.age = age; this.position = position; } greeting() { console.log( `안녕하세요! 저는 ${this.position}을 맡고 있는 ${this.name}입니다!` ); } } class Entertainment extends Idol { constructor(name, age, position, entertainment) { super(name, age, position, entertainment); this.entertainment = entertainment; } } class Testar extends Entertainment { constructor(name, age, position, entertainment, groupGreeting) { super(name, age, position, entertainment); this.groupGreeting = "Take your STAR!"; } } let moondae = new Testar('박문대', 26, '메인 보컬', '오르빗'); moondae.greeting(); // 안녕하세요! 저는 메인 보컬을 맡고 있는 박문대입니다!