ํด๋์ค ์์(ํ์ฅ)
ํด๋์ค ์์์ ์ฌ์ฉํ๋ฉด ํด๋์ค๋ฅผ ๋ค๋ฅธ ํด๋์ค๋ก ํ์ฅํ ์ ์๋ค.
extends
ํด๋์ค ํ์ฅ ๋ฌธ๋ฒ์ผ๋ก ์ฐ๋ฆฌ์ ์์ ๋ฅผ ๋ณด๋ฉด Vehicle์ด๋ผ๋ ํด๋์ค๋ฅผ ์์, ํ์ฅํ๋ ค๊ณ ํ๋ค.
Bicycle๊ณผ Car๋ผ๋ ํด๋์ค๋ก ์ค๋ช ํด๋ณด๊ฒ ๋ค.
class Vehicle {
constructor(name, whell) {
this.name = name
this.whell = whell
}
}
const myVehicle = new Vehicle('์ด์ก์๋จ', 2)
console.log(myVehicle)
๊ธฐ์กด์ ํด๋์ค ํจ์๋ก์จ ์ ๋ฒ ES6 Classes์์ ๋ฐฐ์ด ๋ด์ฉ์ด๋ค.
์ด์ก์๋จ์ผ๋ก์จ Vehicle์ ํด๋์ค๋ก ์ ์ธํ๋ค.
๊ทธ๋ฌ๋ฉด Bicycle์ ์์์ด ๋๋ ๊ฒ์ด๊ณ , Car๋ license๋ผ๋ ๋งค์๋๋ฅผ ์ถ๊ฐํ์ฌ ํ์ฅํ๋ค๊ณ ํํํ๋ ๊ฒ์ด๋ค.
class Bicycle extends Vehicle {
constructor(name, whell) {
super(name, whell)
}
}
// ์์
const myBicycle = new Bicycle('์ผ์ฒ๋ฆฌ', 2)
const daughtersBicycle = new Bicycle('์ธ๋ฐ', 3)
console.log(myBicycle)
console.log(daughtersBicycle)
extends๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด super๋ผ๋ ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก ์๋ ์ฝ๋๋ฅผ ๋ณด๋ฉด super๋ผ๋ ํจ์๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค.
class Car extends Vehicle {
constructor(name, whell) {
super(name, whell)
this.license = license
}
}
// ํ์ฅ
const myCar = new Car('๋ฒค์ธ ', 4, true)
const daughtersCar = new Car('ํฌ๋ฅด์', 4, false)
Car class๋ license ๋งค์๋๊ฐ ์ถ๊ฐ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
super
super ํค์๋๋ ๊ฐ์ฒด์ ๋ถ๋ชจ๊ฐ ๊ฐ์ง๊ณ ์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค.
์ด๋ ํ๋กํ ํ์ ๊ธฐ๋ฐ ์์๋ณด๋ค ์ข์ ์ ์ค ํ๋์ด๋ค.
'๐ค Language > ๐จ JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JS. ์ํ ๊ฐ์ฒด / Math (0) | 2022.02.20 |
---|---|
JS. ๋ฌธ์ (0) | 2022.02.20 |
JS. ES6 Classes (0) | 2022.02.15 |
JS. this (0) | 2022.02.15 |
JS. ์์ฑ์ ํจ์(prototype) (0) | 2022.02.15 |