์ค๋์ Class์ ๋ํด ์กฐ๊ธ ๋ ์์๋ณด๋ ์๊ฐ.
"์์" = Inheritance
Start !
Class๋ ๋ ๋จ์ ์ผ๋ก ์กด์ฌํ ์๋ ์์ง๋ง, ๊ฒฝ์ฐ์ ๋ฐ๋ผ ๋ถ๋ชจ Class, ์์ Class, ์์ Class ์ด๋ฐ์์ผ๋ก ์กด์ฌํ ์๋ ์๋ค.
์์ ๊ทธ๋ฆผ์ฒ๋ผ ๋ถ๋ชจClass ๊ฐ ๊ฐ์ง๊ณ ์๋ ํ์์์(= property) ๋๋ ๋ฉ์๋(= method) ์ ๊ฐ์ ๊ธฐ๋ฅ๋ค์
๋ฐ์ผ๋ก ๋ด๋ ค์ฃผ๋ ๊ฒ์ด ๋ฐ๋ก => ์์(= inheritance) ์ด๋ค.
์๋ก, ์ฌ๋ฌ๊ฐ์ฒด์ Animal์ ๋ง๋ค์ ์๊ฒ ํด์ฃผ๋ ์ค๊ณ๋(= class) ๊ฐ ์๋ค๊ณ ๊ฐ์ ํ๋ค๋ฉด,
animal ์ค์์๋
dog ๋ผ๋ ๊ฐ์ฒด๋ง ๋ง๋ค์ด๋ด๋ class๊ฐ ์์ ์ ์๊ณ ,
cat ์ด๋ผ๋ ๊ฐ์ฒด๋ง ๋ง๋ค์ด๋ด๋ class๋ ์์ ์ ์๋ค.
โ
๊ทธ๋ด ๋ !! ๊ฐ์ฒด์งํฅ์ ์์ฑ ์ค ํ๋์ธ '์์'์ ์ด์ฉํ๋ค๋ฉด,
class dog ๋๋ class cat ์ ๊ฐ๊ฐ ๋ฐ๋ก ์ฝ๋๋ฅผ ๋ง๋๋ ์๊ณ ๋ฅผ ๋์ด๋ ๋๋ค๋ ๊ฒ์ด๋ค.
์๋ฅผ ๋ค์๋ฉด,
โ
animal ์ด๋ผ๋ class ์ name ๊ณผ age๋ฅผ 'ํ์์์' ๋ก ๊ฐ์ง๊ณ ์๊ณ ,
dog ๋ผ๋ ์๋ก์ด class ์๋ name ๊ณผ age ๊ฐ 'ํ์์์'๋ก ๋ค์ด๊ฐ ์์ ์ด๋ผ๋ฉด ~!
โ
์ฐ๋ฆฌ๋ class Dog ์ constructor(= ์์ฑ์ ํจ์)๋ฅผ ๋ค์ ์จ ์ค ํ์๊ฐ ์๋ค๋ ๋ป์ด๋ค.
โ
์ฐ์ , ์ดํด๋ฅผ ๋๊ธฐ ์ํด animal class๋ฅผ ๋ง๋ค์ด๋ณด์.
class Animal {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHi() {
console.log(`๋ด ์ด๋ฆ์ ${this.name} ์ด๊ณ , ๋์ด๋ ๋ฏธ์ด ${this.age} ์งค >_< `);
}
}
name ๊ณผ age๊ฐ property ๊ณ , sayHi ๋ผ๋ method ๋ฅผ ๊ฐ๋ Animal ์ด๋ผ๋ class ๋ฅผ ๋ง๋ค์ด ๋ณด์๋ค.
๋จผ์ , ์ ์ฝ๋๊ฐ ์ง์ฌ์ง๋๋ก ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ ๊ตฌํํด ๋ด๋์ง ํ์ธํด ๋ณธ๋ค.
const animal1 = new Animal('์ถ์์ด', 7);
const animal2 = new Animal('ํฉ์ง์ด', 8);
console.log(animal1);
console.log(animal2);
animal1.sayHi();
animal2.sayHi();
์ด๋ ๊ฒ ํ์ธํด๋ณด๋ฉด,
์ ์์ ์ผ๋ก ๋ด๊ฐ ๋ฃ์ property ์ method ๋๋ก animal ์ด๋ผ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋ด๋๊ฒ์ ํ์ธํ๋ค.
์ฌ๊ธฐ์, ์์ ๊ฐ์ property, method ๋ฅผ ๊ฐ์ง Dog ๋ผ๋ class๋ฅผ ๋ง๋ค๊ณ ์ ํ๋ค๋ฉด,
์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ์งค ์ ์๋ค.
class Animal {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHi() {
console.log(`๋ด ์ด๋ฆ์ ${this.name} ์ด๊ณ , ๋์ด๋ ๋ฏธ์ด ${this.age} ์งค >_< `);
}
}
class Dog extends Animal {}
์๋ ์๋ ์ฝ๋์ ์ถ๊ฐํ๊ฒ์ " class Dog extends Animal " <-- ์ด ํ ์ค ๋ฐ์ ์๋ค.
์ด๊ฒ ๋ง์ผ๋ก๋ Dog ๋ผ๋ ๊ฐ์ฒด๊ฐ ์์ฑ์ด ๋๋์ง ํ์ธํด๋ณด์.
const cuteDog = new Dog('๋์ด', 3);
const prettyPuppy = new Dog('์์', 2);
console.log(cuteDog);
console.log(prettyPuppy);
cuteDog.sayHi();
prettyPuppy.sayHi();
์ด๋ ๊ฒ ํ์ธํ๋ฉด,
Dog ๋ผ๋ ๊ฐ์ฒด๊ฐ sayHi ๋ผ๋ method ๊น์ง ๊ฐ์ง์ฑ๋ก ์์ฑ์ด ์ ๋์๋ค.
์ด๋ ๊ฒ ์์ Class ์ ๊ธฐ๋ฅ๋ค์ ์์๋ฐ์ ํ์ Class ๋ค์ด ๋ฌผ๋ ค๋ฐ๋ ๊ฒ์ ์์์ด๋ผ๊ณ ํ๋ค.
์ด ์์์์ ๊ธฐ์ตํด์ผ ํ ํค์๋๋ 'extends' ์ 'super()' ๊ฐ ์๋ค.
โ
1. extends : ์ฌ์ ์ ์๋ฏธ๋ ์๋ ์ฌ์ง๊ณผ ๊ฐ๋ค.
์ฌ์ฉ๋ฒ์ ์ ์ฝ๋์์ ๋ณด๋๋ฐ์ ๊ฐ์ด,
1). ์๋ก ๋ง๋ค class ์ ๊ฐ์ฒด ๋ช ์์ extends ๋ฅผ ์จ์ฃผ๊ณ
2). extends ๋ค์์๋ ๋ฌผ๋ ค๋ฐ์ ์์ class ๋ช ์ ์ ์ด์ฃผ๋ฉด ๋๋ค.
โ
๊ทธ๋ฌ๋ฉด ๋ถ๋ชจ ํด๋์ค์ property ์ method ๋ฅผ ์์๋ฐ์ ์์ ํด๋์ค๊ฐ ๋ง๋ค์ด ์ง๋ ๊ฒ์ด๋ค.
2. super()
์ฌ์ฉํ ๋ : ๋ถ๋ชจ class ๋ก ๋ถํฐ ์์ ๋ฐ์ property ์ค์, ์ญ์ ๋๋ ์ถ๊ฐํ๊ณ ์ถ์ property ๊ฐ ์๋ ๊ฒฝ์ฐ์ ์ฌ์ฉ.
*์๋ก์ด ๊ฐ์ฒด์ ์ถ๊ฐํ๊ณ ์ถ์ method ๊ฐ ์๋ ๊ฒฝ์ฐ๋ super() ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ,
Dog class ์ {}์์ ์์ sayHi ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ถ๊ฐ๋ฅผ ํด์ฃผ๋ฉด ๋๋ค.
๋๋ ๋ถ๋ชจ class ์๋ ์๋,"gender" ๋ผ๋ porperty ๋ฅผ ์ถ๊ฐํด ๋ณด๊ฒ ๋ค.
์ฌ์ฉ๋ฒ์ ์๋ ์ฝ๋์ ๊ฐ๋ค.
class Animal {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHi() {
console.log(`๋ด ์ด๋ฆ์ ${this.name} ์ด๊ณ , ๋์ด๋ ๋ฏธ์ด ${this.age} ์งค >_< `);
}
}
class Dog extends Animal {
constructor(name, age, gender) {
super(name, age);
this.gender = gender;
}
}
์๋ก์ด ์์ฑ์ํจ์, constructor ๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ , () ์์ ์ถ๊ฐํ "gender" ๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
๊ทธ ๋ค์ {} ์์๋ super() ๋ฅผ ๋ฃ์ด์ฃผ๋๋ฐ,
super() ๋ constructor ์ ๊ฐ๋ค,
๋ค๋ง ์ฌ๊ธฐ์ super ๊ฐ ๊ฐ๋ฆฌํค๋ ๋์์ Dog ๋ผ๋ ๊ฐ์ฒด๊ฐ ์๋,
Animal์ ๊ฐ๋ฆฌํจ๋ค. ๊ทธ๋์ super ์ () ์์๋ Animal ์ property ๊ฐ ๋ค์ด๊ฐ๋ ๊ฒ์ด๋ค.
โ
๊ฐ๋จํ๊ฒ ํ์ด ์ค๋ช ํด๋ณด๋ฉด,
์๋ง ! ๋๋ constructor(name, age, gender) ์ด๋ฐ Dog๋ฅผ ๊ฐ์ง๊ณ ์ถ์ด ~
๊ทธ๋ฌ๋๊น ์๋ง๊ฐ ๋ฌผ๋ ค์ค super(name, age) ๋ ๊ทธ๋๋ก ๊ฐ์ ธ๋ค ์ฐ๊ณ , ๋๋ gender ๋ง ์ถ๊ฐํด์ ๋ง๋ค๊ฒ ~ !!
๋ผ๊ณ , ์์ ๋ถ๋ชจ ํด๋์ค์๊ฒ ์๋ ค์ฃผ๋ ๊ฒ์ด๋ค.
์์
1. constructor ๋ฅผ ๋ฃ์ด์ property๋ฅผ ์ถ๊ฐ ๋๋ ์ญ์ , ๋ณ๊ฒฝ ํด์ค๋ค.
2. super ๋ฅผ ํตํด์ ์์ ํด๋์ค์ ์๋ ค์ค๋ค.
3. ๊ทธ ๋ค์์ this.gender ๋ก ๊ฐ์ฒด์ง์ ์ ํด์ค๋ค.
โ
super() ๋ฅผ ์ฌ์ฉํ๋ฉด์ ์ ์ํ ์ :
1. super ๋ฅผ constructor, ์์ฑ์ ํจ์ {}๋ด๋ถ์ ์จ์ค์ผ ํ๊ณ ,
2. ์๋ก์ด ์ถ๊ฐํ property ๋ฅผ this ๋ก ์ง์ ํด์ฃผ๊ธฐ ์ ์ ์ฌ์ฉํด์ฃผ์ด์ผ ํ๋ค.
โ
๊ทธ๋ ์ง ์๋ค๋ฉด ์ด๋ฐ ์ค๋ฅ๋ฅผ ๋ง๋๊ฒ ๋ ๊ฒ์ด๋ค.
โ
ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor at new Dog === (์๋ ์ฝ๋๋๋ก ์ฝ๋ฉ์ ํ๋ค๋ฉด, ๋ง๋๊ฒ ๋๋ ์ค๋ฅ์ด๋ค)
class Dog extends Animal {
constructor(name, age, gender) {
this.gender = gender;
super(name, age);
}
}
ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
at new Dog (/Users/yoonzero/Desktop/Sparta/Week_05/20.js:14:9)
at Object.<anonymous> (/Users/yoonzero/Desktop/Sparta/Week_05/20.js:26:17)
ํด์ : 'this' ๋ก ์ง์ ๋๋, new Dog์ ํ์๋ ์์ฑ์๋ก ๋ฐํ ๋๊ธฐ ์ ์, ํ์๋ ํด๋์ค(= Dog) ์์์ super๋ฅผ ์ธ์ณ์ผ ํ๋ค.
โ
์ ์ฝ๋๋ this ๋ก gender ๋ฅผ ์ง์ ํ ํ์ super ๋ฅผ ํธ์ถํ๊ธฐ ๋๋ฌธ์,
์๋งํํ ๋ง์ ์ํ๊ณ ๋ด ๋ง๋๋ก ์ง์ ํด ์ด๊ฒ๊ณผ ๊ฐ๋ค. === ์ ์กฐ์น ํ๋ณด๊ณ .
์ด์ super() ๋ฅผ ์ฌ์ฉํด ๋ด๊ฐ ์ํ Dog์ด ๋ง๋ค์ด์ก๋์ง ํ์ธํด๋ณด์.
const cuteDog = new Dog('๋์ด', 3, 'female');
const prettyPuppy = new Dog('์์', 2, 'male');
console.log(cuteDog);
console.log(prettyPuppy);
์์ ๊ฐ์ด ํ์ธํด๋ณธ๋ค,
๋ถ๋ชจํด๋์ค์๋ ์๋ gender ๊ฐ ์ถ๊ฐ๋์ด ์ ๋ง๋ค์ด์ง๊ฒ์ ํ์ธํ ์ ์๋ค.
์ค๋์ class ์ ์์ ๊ธฐ๋ฅ์ ๋ํด์ ์์๋ณด์๋ค.
์์์ ์ ์ด์ฉํ๋ค๋ฉด , ๋ณด๋ค ํจ์จ์ ์ผ๋ก ์ํ๋ ๊ฐ์ฒด๋ค์ ์ต์ ๋ฑ์ ์์ ํ๋ฉด์ ๋ฝ์๋ด๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค.
์์ <-> ํ์ ๊ฐ๋ ์ ๊ฐ์ง ๊ฐ์ฒด๋ค์ ์์ฑํด์ผ ํ๋ค๋ฉด, ์์ ๊ธฐ๋ฅ์ ์ด์ฉํด์ผ ๊ฒ ๋ค.
๋ฌธ : ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor at new Dog
์ : super() ์ ์์น๋ฅผ constructor ํจ์์ ๋ฐ์ผ๋ก๋ ์ด๋์์ผ๋ณด๊ณ , this ๋ฅผ ์ง์ ํ๊ธฐ ์ ์ super๋ฅผ ๋ฃ์ด์ฃผ์๋ค.
ํด : super() ๋ constructor ํจ์์ {}์์ ์์นํด์ผ ํ๋ฉฐ, ๋ถ๋ชจ๋ก๋ถํฐ ์์๋ฐ์ property ๋ฅผ ๋งํด์ฃผ๊ณ , this ๋ก ์ถ๊ฐํ property ๋ฅผ ์ง์ ํด ์ฃผ์ด์ผ ํ๋ค.
์ : class ์ ์๋ก์ด ๊ธฐ๋ฅ์ธ ์์์ ์๊ฒ๋๋ฉด์, ๋ด๊ฐ ์ค๋๊น์ง ์์๋ ๊ฐ๋ ๋ณด๋ค ํ์ฅ๋ class ์ ๊ฐ๋ ์ ์ดํดํ๊ฒ ๋์๋ค. super() ์ ์ฌ์ฉ๋ฒ, ์๋ก์ด Error ๋ฉ์์ง.
โ
โ
#javascript #๋ฌธ๋ฒ #class #super #์์ #ํ์ํด๋์ค #์์ํด๋์ค #๋ถ๋ชจํด๋์ค #๋ฌธ์ํด์
#ReferenceError #์ค๋ฅ #์ค๋ฅํด๊ฒฐ #this #์์_์ฃผ์