์์, Inheritance
- ์์์ ๊ฐ์ฒด๋ค ๊ฐ์ ๊ด๊ณ๋ฅผ ๊ตฌ์ถํ๋ ๋ฐฉ๋ฒ์ด๋ค.
- ์ํผ ํด๋์ค(๋๋ ๋ถ๋ชจ ํด๋์ค)๋ฑ์ ๊ธฐ์กด์ ํด๋์ค๋ก๋ถํฐ ์์ฑ๊ณผ ๋์์ ์์๋ฐ์ ์ ์๋ค.
- ๊ณตํต๋๋ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฆฌํ ์ ์๋ ๋ฐฉ๋ฒ์ด๋ค.
class Person {
name;
year;
constructor(name, year) {
this.name = name;
this.year = year;
}
}
class Singer extends Person {
sing() {
return `๋
ธ๋ํฉ๋๋ค.`;
}
}
class Actor extends Person {
acting() {
return `์ฐ๊ธฐํฉ๋๋ค.`;
}
}
const zico = new Singer("zico", 1993);
console.log(zico); // Singer { name: 'zico', year: 1993 }
const chungAh = new Actor("chungAhLee", 1984);
console.log(chungAh); // Actor { name: 'chungAhLee', year: 1984 }
console.log(zico.sing()); // ๋
ธ๋ํฉ๋๋ค.
console.log(chungAh.acting()); // ์ฐ๊ธฐํฉ๋๋ค.
// ์๋ ์๋ก ๊ด๊ณ๊ฐ ์์ผ๋ ์ฌ์ฉํ ์ ์๋ค.
// console.log(chungAh.sing())
const soo = new Person("soo", 1997);
// ๋ถ๋ชจ๊ฐ ์์์๊ฒ ์์์ ๊ฐ๋ฅํ๋
console.log(soo.name); // soo
console.log(soo.year); // 1997
// ์์์ ๋ถ๋ชจ์๊ฒ ์์ํด์ฃผ๋๊ฑด ์๋๋ค.
// console.log(soo.sing())
// ๊ฐ์ ๋งฅ๋ฝ
console.log(chungAh instanceof Person); // true
console.log(chungAh instanceof Actor); // true
console.log(chungAh instanceof Singer); // false