๐Ÿ“Œ Class (์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ES6 ์ŠคํŽ™)

โฌ…๏ธ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ†ตํ•œ ๊ฐ์ฒด ์ƒ์„ฑ(๋ณต์Šต)

const User = function (name, age) {
    this.name = name;
    this.age = age;
    this.showName = function () {
       console.log(this.name);  
  };
};

const mike = new User(โ€˜Mikeโ€™, 30);
console.log(mike); // { name: โ€˜Mikeโ€™, age: 30, showName: [Function(ananymous)] }

โžก๏ธ class ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•œ ๊ฐ์ฒด ์ƒ์„ฑ(ES6)

class User2 {
    constructor (name, age) {
      this.name = name;
      this.age = age;
    }
    showName() {
        console.log(this.name); 
    }
}

const tom = new User2(โ€˜Tomโ€™, 19);
console.log(tom); { name: โ€˜Tomโ€™, age: 19 }
// ์œ„ ์˜ˆ์ œ์™€ ๋‹ฌ๋ฆฌ class ์—์„œ ๊ฐ์ฒด๋ฅผ ์ฐ์–ด๋ณด๋ฉด ๋ฉ”์„œ๋“œ๋Š” ๋ณด์ด์ง€ ์•Š์Œ
  • new๋กœ ํ˜ธ์ถœ ํ–ˆ์„๋•Œ ๋‚ด๋ถ€์— ์ •์˜๋œ ๋‚ด์šฉ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ๋™์ผํ•˜๋‹ค.
  • class ํ‚ค์›Œ๋“œ์™€ constructor(์ƒ์„ฑ์ž) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • showName() ์ฒ˜๋Ÿผ class ๋‚ด์— ์ •์˜ํ•œ ๋ฉ”์„œ๋“œ๋Š” User2 ํ”„๋กœํ† ํƒ€์ž…(์ƒ์œ„๊ฐ์ฒด)์— ์ €์žฅ ๋œ๋‹ค.โ€˜
  • ๋•Œ๋ฌธ์— mike๋Š” ๊ฐ์ฒด ๋‚ด๋ถ€์— showName()์ด ์žˆ๋‹ค.
  • tom์€ ํ”„๋กœํ† ํƒ€์ž… ๋‚ด๋ถ€์— showName()์ด ์žˆ๋‹ค.
  • ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘˜๋‹ค mike.showName(), tom.showName()์œผ๋กœ ๋™์ผํ•˜๋‹ค.

๐Ÿ”” class๊ฐ€ ์•„๋‹Œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ prototype์— ๋ฉ”์„œ๋“œ ๋งŒ๋“ค์–ด class์™€ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

const User3 = function (name, age) {
    this.name = name;
    this.age = age;
}

User3.prototype.showName = function () {
    console.log(this.name);
}

const jimmy = new User3 (โ€˜Jimmyโ€™, 25);

console.log(jimmy); // { name: โ€˜Jimmyโ€™, age: 25 }

// ์œ„์™€๊ฐ™์ด ์ž‘์„ฑ ์‹œ class์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฉ”์„œ๋“œ๊ฐ€ prototype์œผ๋กœ ํ• ๋‹น๋œ๋‹ค.

๐Ÿ’ก๋‹จ์ˆœํžˆ ๋ฌธ๋ฒ•์˜ ํŽธ์˜์„ฑ ๋•Œ๋ฌธ์— class๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฑธ๊นŒ?

  • ๊ฐ ๋ฐฉ์‹์—์„œ ํ˜ธ์ถœ์‹œ new๋ฅผ ๋นผ๊ณ  ํ˜ธ์ถœํ•œ๋‹ค๋ฉด?
// ์ƒ์„ฑ์ž ํ•จ์ˆ˜
const User = function (name, age) {
    this.name = name;
    this.age = age;
    this.showName = function () {
       console.log(this.name);  
  };
};

const mike = User(โ€˜Mikeโ€™, 30);

console.log(mike); // undefined

๐Ÿ‘‰ new ๋ฅผ ๋นผ๊ณ  ํ˜ธ์ถœ์‹œ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (์—๋Ÿฌ๊ฐ€ ์•ˆ๋‚จ)

// class
class User {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    showName() {
        console.log(this.name);
    }
}

const eddy = User(โ€˜Eddyโ€™, 17); // TypeError

๐Ÿ‘‰ new๋ฅผ ๋นผ๊ณ  ํ˜ธ์ถœ์‹œ TypeError๋ฅผ ๋ฐ˜ํ™˜ํ•จ (์—๋Ÿฌ ๋ฐœ์ƒ)
์ด๋Š” ์ธ์ ์‹ค์ˆ˜๋กœ ์ธํ•œ ์›ํ•˜์ง€ ์•Š๋Š” ๋™์ž‘์„ ๋ฐฉ์ง€ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • for ..in ๋ฌธ์œผ๋กœ ์ˆœํšŒ ํ•  ์‹œ ๋ฉ”์„œ๋“œ๊ฐ€ ํฌํ•จ ๋ ๊นŒ?
// ๋ฐ”๋กœ ์œ„ ์˜ˆ์‹œ ์ฝ”๋“œ์— ์ด์–ด์„œ
for (let key in eddy) {
    console.log(key);
}

// name, age

๐Ÿ‘‰ class ๋‚ด๋ถ€์— ์„ ์–ธํ•œ ๋ฉ”์„œ๋“œ๋Š” for..in๋ฌธ์—์„œ ์ œ์™ธ๋œ๋‹ค.
๐Ÿ‘‰ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์—์„œ๋Š” ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ๋งŒ ํ™•์ธ ํ•˜๊ธฐ ์œ„ํ•ด hasOwnProperty() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํ–ˆ์ง€๋งŒ class๋Š” ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†๋‹ค.

๐Ÿ“Œ ์ƒ์†

์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” prototype์œผ๋กœ ์ƒ์† ๊ตฌํ˜„
class๋Š” extend ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉ

class Car {
    constructor(color) {
        this.color = color;
        this.wheels = 4;
    }
    drive () {
        console.log(โ€œdriveโ€ฆโ€);
    }
}

class Bmw extend Car {
    stop () {
        console.log(โ€œstop!โ€);
    }
}

const x5 = new Bmw(โ€˜blueโ€™);
console.log(x5); // { color: โ€˜blueโ€™, wheels: 4  }
x5.drive(); // โ€˜driveโ€ฆโ€™
x5.stop(); // โ€˜stop!โ€™
  • ์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด Car๋ฅผ ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ๋ณด๊ณ  ์ด๋ฅผ ์ƒ์† ๋ฐ›์•„ Bmw ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ์ž ์—ญํ• ์„ ํ•˜์—ฌ x5๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

  • ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

  • drive()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์šฐ์„  x5 ๊ฐ์ฒด์—์„œ ์ฐพ๊ณ  ์—†์œผ๋ฉด prototype์—์„œ ์ฐพ๊ณ  ์—ฌ๊ธฐ์—๋„ ์—†์œผ๋ฉด ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ prototype์—์„œ ์ฐพ๋Š”๋‹ค.

  • ์œ„์—๋„ ์„ค๋ช… ํ–ˆ๋“ฏ์ด class ํ‚ค์›Œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ํ”„๋กœํ† ํƒ€์ž…์— ์ €์žฅํ•œ๋‹ค.

    ๐Ÿ“์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : ์ฝ”๋”ฉ์•™๋งˆ ์œ ํŠœ๋ธŒ

  • ์œ„ ์ด๋ฏธ์ง€์—์„œ ๋ณด๋“ฏ์ด(๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์™€ ์ด๋ฆ„์€ ๋‹ค๋ฆ„) ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ๋ฉ”์„œ๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ๊ณ  ๊ทธ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์ตœ์ƒ์œ„ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค. (์„ค๋ช…์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค)

๐Ÿ“Œ method overriding (๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ)

๐Ÿ”” ๋งŒ์•ฝ ๋ถ€๋ชจ์˜ ๋ฉ”์„œ๋“œ์™€ ๋™์ผํ•œ ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ?

class Car {
    constructor(color){
         this.color = color;
    }
    drive() {
        console.log(โ€œdriveโ€ฆโ€);
    }
    stop() {
        console.log(โ€œstop!โ€);
    }
}

class Audi extend Car {
    stop() {
       console.log(โ€œOFFโ€);
    }
}

const a8 = new Audi (โ€œgrayโ€);
a8.stop(); // OFF
  • ์œ„ ์˜ˆ์‹œ์—์„œ ๋ณด๋“ฏ์ด ๋ถ€๋ชจ ๋ฉ”์„œ๋“œ์™€ ์ž์‹ ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์ด ๋™์ผํ•˜๋‹ค.
  • ์ด ๊ฒฝ์šฐ ์ž์‹ ๋ฉ”์„œ๋“œ๋กœ ๋ฎ์–ด ์”Œ์›Œ์ง„๋‹ค.

๐Ÿ‘‰ ๋งŒ์•ฝ ๋ถ€๋ชจ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด์„œ ํ™•์žฅ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด super๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.


class Car {
    constructor(color){
         this.color = color;
    }
    drive() {
        console.log(โ€œdriveโ€ฆโ€);
    }
    stop() {
        console.log(โ€œstop!โ€);
    }
}

class Audi extend Car {
    stop() {
       super.stop(); // ์ด ๋ถ€๋ถ„ ์ฝ”๋“œ๊ฐ€ ์ถ”๊ฐ€๋จ
       console.log(โ€œOFFโ€);
    }
}

const a8 = new Audi (โ€œgrayโ€);
a8.stop(); // โ€œstop!โ€ โ€œOFFโ€
  • super ์˜ ๋™์ผํ•œ ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œstop()์„ ํ˜ธ์ถœํ•˜๋ฉด ๋ฎ์–ด ์”Œ์šฐ์ง€ ์•Š๊ณ  ํ™•์žฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ“Œ class overriding(์ƒ์„ฑ์ž ์˜ค๋ฒ„๋ผ์ด๋”ฉ)

class Car {
    constructor(color) {
        this.color = color;
    }
    drive() {
         console.log(โ€œdriveโ€ฆโ€);
    }
}

class Benz extends Car {
    this.navigation = 1;
}

const sclass = new Benz (white);

console.log(sclass); // error
  • ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ƒˆ๋กœ์šด ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ ํ•œ๋‹ค.
  • ์ž์‹ ํด๋ž˜์Šค๋Š” constructor๊ฐ€ ์—†๋‹ค.
  • ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑ์‹œ error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

class Car {
    constructor(color) {
        this.color = color;
    }
    drive() {
         console.log(โ€œdriveโ€ฆโ€);
    }
}

class Benz extends Car {
    constructor() {           // constructor ์ถ”๊ฐ€
        super();              // super ํ‚ค์›Œ๋“œ ์ถ”๊ฐ€
        this.navigation = 1;
    }
}

const sclass = new Benz (white);

console.log(sclass); // { color: undefined, navigation: 1 }
  • constructor ๋ฐ super() ๋ฉ”์„œ๋“œ๊ฐ€ ์ถ”๊ฐ€ ๋˜์—ˆ๋‹ค.
  • ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ถ”๊ฐ€๋œ ํ”„๋กœํผํ‹ฐ๋Š” ์ •์ƒ์ ์œผ๋กœ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.
  • ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ color๊ฐ’์ด undefined๋กœ ์ฐํžŒ๋‹ค.
  • ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ๋ฐ›์€ ์ธ์ž๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ›์•„์™€์•ผ ํ•œ๋‹ค.

class Car {
    constructor(color) {
        this.color = color;
    }
    drive() {
         console.log(โ€œdriveโ€ฆโ€);
    }
}

class Benz extends Car {
    constructor(color) {           // ์ธ์ˆ˜ ์ถ”๊ฐ€
        super(color);              // ์ธ์ˆ˜ ์ถ”๊ฐ€
        this.navigation = 1;
    }
}

const sclass = new Benz (white);

console.log(sclass); // { color: white, navigation: 1 }
  • ์ž์‹ ํด๋ž˜์Šค์— constructor์— color๋ฅผ ์ „๋‹ฌํ•ด ์ฃผ๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.
profile
ํ’€์Šคํƒ ๊ฐœ๋ฐœ์ž ์ง€๋ง์ƒ (React, Node.js, AWS, Git, Github, Github Action, Docker)

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด

Powered by GraphCDN, the GraphQL CDN