๋ชฉ์ฐจ
Super
- ๋ถ๋ชจ ํด๋์ค
- super ํค์๋๋ constructor ์์์๋ ํจ์์์๋ง ์ฌ์ฉํ ์ ์๋ค.
class Person {
name;
year;
constructor(name, year) {
this.name = name;
this.year = year;
}
sayHello() {
return `Hello I'm ${this.name}`;
}
}
class Singer extends Person {
sing() {
return `๋
ธ๋ํฉ๋๋ค.`;
}
constructor(name, year, part) {
// ๋ถ๋ชจ ํด๋์ค
// Person() ์ด๊ฑฐ๋ ๋๊ฐ์
super(name, year);
this.part = part;
}
sayHello() {
// undefined์
๋๋ค. rap ํํธ์
๋๋ค.
// return `${super.name}์
๋๋ค. ${this.part} ํํธ์
๋๋ค.`;
// super.name์ด ์๋ this.name์ผ๋ก ํด์ค์ผํ๋ค
// constructor๊ฐ ์๋ ๊ณณ์์๋ super ๋ง๊ณ this๋ก ํด์ค์ผํ๋ค!!
return `${this.name}์
๋๋ค. ${this.part} ํํธ์
๋๋ค.`; // zico์
๋๋ค. rap ํํธ์
๋๋ค.
}
// ๋ถ๋ชจ ํด๋์ค์ ์๋ sayHello()๋ฅผ ์ฐ๋ ค๋ฉด?
// super ํค์๋๋ ํจ์์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
superSayHello() {
return `${super.sayHello()}`;
}
}
class Actor extends Person {
acting() {
return `์ฐ๊ธฐํฉ๋๋ค.`;
}
}
const ian = new Person("DPR Ian", 1990);
console.log(ian.sayHello()); // Hello I'm DPR Ian
const zico = new Singer("zico", 1993, "rap");
console.log(zico); // Singer { name: 'zico', year: 1993, part: 'rap' }
console.log(zico.sayHello()); // zico์
๋๋ค. rap ํํธ์
๋๋ค.
console.log(zico.superSayHello()); // Hello I'm zico
Super
- ๋ถ๋ชจ ํด๋์ค
- super ํค์๋๋ constructor ์์์๋ ํจ์์์๋ง ์ฌ์ฉํ ์ ์๋ค.
class Person {
name;
year;
constructor(name, year) {
this.name = name;
this.year = year;
}
sayHello() {
return `Hello I'm ${this.name}`;
}
}
class Singer extends Person {
sing() {
return `๋
ธ๋ํฉ๋๋ค.`;
}
constructor(name, year, part) {
// ๋ถ๋ชจ ํด๋์ค
// Person() ์ด๊ฑฐ๋ ๋๊ฐ์
super(name, year);
this.part = part;
}
sayHello() {
// undefined์
๋๋ค. rap ํํธ์
๋๋ค.
// return `${super.name}์
๋๋ค. ${this.part} ํํธ์
๋๋ค.`;
// super.name์ด ์๋ this.name์ผ๋ก ํด์ค์ผํ๋ค
// constructor๊ฐ ์๋ ๊ณณ์์๋ super ๋ง๊ณ this๋ก ํด์ค์ผํ๋ค!!
return `${this.name}์
๋๋ค. ${this.part} ํํธ์
๋๋ค.`; // zico์
๋๋ค. rap ํํธ์
๋๋ค.
}
// ๋ถ๋ชจ ํด๋์ค์ ์๋ sayHello()๋ฅผ ์ฐ๋ ค๋ฉด?
// super ํค์๋๋ ํจ์์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
superSayHello() {
return `${super.sayHello()}`;
}
}
class Actor extends Person {
acting() {
return `์ฐ๊ธฐํฉ๋๋ค.`;
}
}
const ian = new Person("DPR Ian", 1990);
console.log(ian.sayHello()); // Hello I'm DPR Ian
const zico = new Singer("zico", 1993, "rap");
console.log(zico); // Singer { name: 'zico', year: 1993, part: 'rap' }
console.log(zico.sayHello()); // zico์
๋๋ค. rap ํํธ์
๋๋ค.
console.log(zico.superSayHello()); // Hello I'm zico