* class: ES6์ ์ถ๊ฐ๋ ์คํ. class๋ผ๋ ํค์๋๋ฅผ ์ฌ์ฉํ๊ณ ๋ด๋ถ์ constructor๊ฐ ์์. new๋ฅผ ํตํด์ ํธ์ถํ๋ฉด ์๋์ผ๋ก ์คํ๋จ.
- class์ ์๋ constructor ๋ถ๋ถ์ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ํ ๊ฐ์ผ๋ก ์ธ์๋ฅผ ๋๊ฒจ๋ฐ์ ์ ์๋๋ฐ ์ฌ๊ธฐ์๋ name๊ณผ age๊ฐ ๋ง๋ค์ด์ง
- showName์ฒ๋ผ ํด๋์ค๋ฅผ ์ ์ํ ๋ฉ์๋๋ User2์ ํ๋กํ ํ์ ์ ์๋ผ๋.
const User = function(name,age){
this.name = name;
this.age = age;
this.showName = function () {
console.log(this.name);
};
};
const mike = new User("Mike",30);
class User2 {
constructor(name,age){
this.name = name;
this.age = age;
}
showName(){
console.log(this.name);
}
}
const tom = new User2("Tom",19);
=> ๊ทธ๋์ mike๋ ๊ฐ์ฒด ๋ด๋ถ์ showName์ด ์๊ณ ,
Tom์ ํ๋กํ ํ์ ๋ด๋ถ์ showName์ด ์์
์ฌ์ฉ๋ฒ์ mike.showName(); , tom.showName(); ๋์ผ
- class๋ก ๋ง๋ class User2์์ new๋ฅผ ๋นผ๊ณ ์คํํ ๊ฒฝ์ฐ console์ฐฝ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋๋ก ์ค๊ณ๋์ด ์์.
- ์์ฑ์ํจ์์์ ์ด ๋ถ๋ถ์ class๋ก ๋์ผํ๊ฒ ๋ง๋ค๊ธฐ
const User = function(name,age){
this.name = name;
this.age = age;
// this.showName = function () {
// console.log(this.name);
// };
};
User.prototype.showName = function(){
console.log(this.name);
}
const mike = new User("Mike",30);
- new๋ฅผ ๋นผ๊ณ ์คํํ ๊ฒฝ์ฐ undefined๊ฐ ๋์ค๋๋ฐ Userํจ์๊ฐ ๋ฐํํ๋ ๊ฐ์์ return๋ฌธ์ด ์์ผ๋ฏ๋ก ์๋ฌด๊ฒ๋ ๋ฐํํ์ง ์๊ธฐ ๋๋ฌธ. ( const mike = User("Mike",30)์์ ๋ฐํํ ๊ฐ์ด undefined๊ณ ๊ทธ ๊ฐ์ด mike๋ก ๋ค์ด๊ฐ)
- for in ๋ฌธ ์ฌ์ฉํด ์ํํ ๊ฒฝ์ฐ mike๋ name, age, showName ๋ค ๋์ค์ง๋ง tom์ ๊ฒฝ์ฐ์๋ name๊ณผ age๋ง ๋์ด
<- for in๋ฌธ์ ํ๋กํ ํ์ ์ ํฌํจ๋ ํ๋กํผํฐ๋ค์ ๋ค ๋ณด์ฌ์ฃผ๊ณ ๊ฐ์ฒด๊ฐ ๊ฐ๊ณ ์๋ ํ๋กํผํฐ๋ฅผ ํ๋ณํ๊ธฐ ์ํด์ hasOwnproperty๋ฅผ ์ฌ์ฉํด์ผ ํ๋๋ฐ class ๋ฉ์๋๋ for in๋ฌธ์์ ์ ์ธ๋จ
* ์์ฑ์ํจ์๋ ํ๋กํ ํ์ ์ ์ด์ฉํด ์์์ ๊ตฌํํ์๋๋ฐ class์์์ ์์์ extends ํค์๋ ์ฌ์ฉ
- car class์์ ์ ์ธํ color์ wheels๊ฐ ๋ค์ด๊ฐ์๊ณ ํ๋กํ ํ์ ์์๋ park๊ฐ ์์.
- ํด๋์ค ๋ด๋ถ์์ ์ ์ธํ ๋ฉ์๋๋ ํ๋กํ ํ์ ๋ฐ์ผ๋ก ๋ค์ด๊ฐ๋๋ฐ ์ฌ๊ธฐ์ drive์ stop์ด ์์.
class car{
constructor(color){
this.color = color;
this.wheels = 4;
}
drive(){
console.log("drive..");
}
stop(){
console.log("stop!");
}
}
//car๋ฅผ ์์ํด์ bmw ๋ง๋ค๊ธฐ
class Bmw extends car{
park(){
console.log("park");
}
}
const z4 = new Bmw("blue");
* Class - ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ(method overriding)
- ๋์ผํ ๋ฉ์๋๊ฐ ์กด์ฌํ ๊ฒฝ์ฐ ๋ฎ์ด์ฐ๊ธฐ๊ฐ ๋จ.
- ๋ง์ฝ ๊ธฐ์กด์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ ๊ฒฝ์ฐ super๋ฅผ ์ฌ์ฉ. -> stop๊ณผ no๊ฐ ๊ฐ์ด ์ถ๋ ฅ
class Car{
constructor(color){
this.color = color;
this.wheels = 4;
}
drive(){
console.log("drive..");
}
stop(){
console.log("stop!");
}
}
class Bmw extends Car{
park(){
console.log("park");
}
stop(){
super.stop();
console.log("NO");
}
}
const z4 = new Bmw("blue");
=> ์ด๋ ๊ฒ super.๋ฉ์๋ ์ด์ฉํด์ ๋ถ๋ชจ class์ ์ ์๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์.
*Class - ์ค๋ฒ๋ผ์ด๋ฉ(overriding) (์์ฑ์ ์ค๋ฒ๋ผ์ด๋ฉ)
- constructor์์ this๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ super constructor ์ฆ ๋ถ๋ชจ ์์ฑ์๋ฅผ ๋ฐ๋์ ํธ์ถํด์ผ ํจ
- class์ constructor๋ ๋น ๊ฐ์ฒด๋ก ๋ง๋ค์ด์ง๊ณ this๋ก ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํค๊ฒ ๋จ. ๋ฐ๋ฉด์ extends ์จ์ ๋ง๋ ์์ ํด๋์ค๋ ๋น ๊ฐ์ฒด๊ฐ ๋ง๋ค์ด์ง๊ณ this์ ํ ๋นํ๋ ์์ ์ ๊ฑด๋๋ฐ๊ธฐ ๋๋ฌธ์ ํญ์ super();๋ฅผ ์ด์ฉํ์ฌ ๋ถ๋ชจํด๋์ค์ constructor๋ฅผ ์คํํด์ค์ผ ํจ.
class Car{
constructor(color){
this.color = color;
this.wheels = 4;
}
drive(){
console.log("drive..");
}
stop(){
console.log("stop!");
}
}
class Bmw extends Car{
constructor(){
super();
this.navigation = 1;
}
park(){
console.log("park");
}
}
// const z4 = new Bmw("blue"); -> undefined
- super(); ์ฐ์ง ์๋ ๊ฒฝ์ฐ ์๋ฌ๊ฐ ๋ฐ์
class Car{
constructor(color){
this.color = color;
this.wheels = 4;
}
drive(){
console.log("drive..");
}
stop(){
console.log("stop!");
}
}
class Bmw extends Car{
constructor(){
this.navigation = 1;
}
park(){
console.log("park");
}
}
// const z4 = new Bmw("blue"); -> ์๋ฌ๋ฐ์
- ์ ๋๋ก ๋์ํ๊ธฐ ์ํด์๋ ์์ ํด๋์ค์ constructor์ ๋์ผํ ์ธ์๋ฅผ ์ถ๊ฐํด์ค์ผ ํจ
constructor๊ฐ ์์ผ๋ฉด ์๋ ๊ฒ์ฒ๋ผ ํ๋ํด์ ์์์์ฑ์๋ ๋ถ๋ชจ ์์ฑ์์ ์๋ ๊ฒ์ ๋ฌด์กฐ๊ฑด ํธ์ถํด์ผํจ
์์์์ฑ์์ constructor๊ฐ ์์ผ๋ฉด super๋ฅผ ์ด์ฉํด ํธ์ถํด์ฃผ๊ณ this.ํ๋กํผํฐ๋ก ํ ๋นํด์ค์ผ ํจ
class Car{
constructor(color){
this.color = color;
this.wheels = 4;
}
drive(){
console.log("drive..");
}
stop(){
console.log("stop!");
}
}
class Bmw extends Car{
constructor(color){
super(color);
this.navigation = 1;
}
park(){
console.log("park");
}
}
// const z4 = new Bmw("blue"); -> z4 / Bmw {color: 'blue', wheels: 4, navigation: 1}
'js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
javascript async, await (0) | 2022.11.01 |
---|---|
javascript ํ๋ก๋ฏธ์ค (Promise) (0) | 2022.11.01 |
javascript ์์, ํ๋กํ ํ์ (Prototype) (0) | 2022.10.31 |
javascript ๋ฉ์๋ call, apply, bind (0) | 2022.10.31 |
javascript setTimeout / setInterval (0) | 2022.10.31 |