Demystifying ES6 Classes And Prototypal Inheritance - Lee-hyuna/33-js-concepts-kr GitHub Wiki

ES6 ํด๋ž˜์Šค์™€ ํ”„๋กœํ† ํƒ€์ž… ์ƒ์† ์ดํ•ดํ•˜๊ธฐ

JavaScript์–ธ์–ด์˜ ์ดˆ๊ธฐ ์—ญ์‚ฌ์—์„œ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด์™€ ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ์ ์ ˆํ•œ ๊ตฌ๋ฌธ์ด ๋ถ€์กฑํ•˜์—ฌ ์ ๋Œ€๊ฐ์˜ ๊ตฌ๋ฆ„์ด ํ˜•์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 2015๋…„ ES6๊ทœ๊ฒฉ์ด ๊ณต๊ฐœ๋˜๊ธฐ ์ „๊นŒ์ง€ classํ‚ค์›Œ๋“œ๊ฐ€ ๋„์ž…๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ๊ธฐ์กด์˜ ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜ ์ƒ์†์— ๋Œ€ํ•œ ๊ตฌ๋ฌธ์  ์„คํƒ•(syntactical sugar)์œผ๋กœ ๋ฌ˜์‚ฌ๋ฉ๋‹ˆ๋‹ค.

๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ˆ˜์ค€์—์„œ,ES6์˜ classํ‚ค์›Œ๋“œ๋Š” ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜ ์ƒ์†์— ๋ถ€ํ•ฉํ•˜๋Š” ์ƒ์„ฑ์žํ•จ์ˆ˜ ์ •์˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ์‹ธ๊ธฐ ์œ„ํ•ด new ํ‚ค์›Œ๋“œ๊ฐ€ ๋„์ž…๋˜์—ˆ์ง€๋งŒ ์ฝ์„ ์ˆ˜์žˆ๋Š” ์ฝ”๋“œ๋กœ ์ด์–ด์ง€๊ณ  ๋ฏธ๋ž˜์˜ ๊ฐ์ฒด ์ง€ํ–ฅ์ ์ธ ๊ธฐ๋Šฅ์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฐ˜์„ ๋งˆ๋ จํ•˜๋Š” ๊ฒƒ์€ ์ค‘๋ณต์ ์œผ๋กœ ๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ES6์ด์ „์— ๋™์ผํ•œ ์œ ํ˜•์˜ ๋งŽ์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ์œ„ํ•œ blueprint(ํด๋ž˜์Šค)์„ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

function Animal (name, fierce) {
  Object.defineProperty(this, 'name', {
    get: function() { return name; }
  });

  Object.defineProperty(this, 'fierce', {
    get: function() { return fierce; }
  });
}

Animal.prototype.toString = function() {
  return 'A' + ' ' + ( this.fierce ? 'fierce' : 'tame' ) + ' ' + this.name;
}

์ด๊ฒƒ์€ Animalํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ์œ„ํ•œ blueprint์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ„๋‹จํ•œ ๊ฐ์ฒด ์ƒ์„ฑ์ž์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ƒ์„ฑ์žํ•จ์ˆ˜์— ๋‘ ๊ฐœ์˜ ์ฝ๊ธฐ ์ „์šฉ ownํ”„๋ผํผํ‹ฐ์™€ ์ปค์Šคํ…€toString ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. new ํ‚ค์›Œ๋“œ๋กœ Animal์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค :

var Lion = new Animal('Lion', true);
console.log(Lion.toString()); // "A fierce Lion"

์ข‹์•„! ๊ทธ๊ฒƒ์€ ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๊ฒฐํ•œ ๋ฒ„์ „์„ ์œ„ํ•ด ES6ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Animal {

  constructor(name, fierce){
      this._name = name;
      this._fierce = fierce;
  }

  get name() {
    return this._name;
  }

  get fierce() {
    return ` This animal is ${ this._fierce ? 'fierce' : 'tame' }`;
  }

  toString() {
    return `This is a ${ this._fierce ? 'fierce' : 'tame' } ${this._name}`;
  }

}

์ด์ „์ฒ˜๋Ÿผ new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Animalํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

let Lion = new Animal('Lion', true);

    console.log(Lion.fierce); 

    console.log(Lion.toString())

ES6์—์„œ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋ฉฐ ๊ฐ์ฒด ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์ „ ์‹œ๋ฎฌ๋ ˆ์ด์…˜๋ณด๋‹ค ๊ฐ์ฒด ์ง€ํ–ฅ์ ์ธ ๋Š๋‚Œ์ด ๋” ์ž์—ฐ์Šค๋Ÿฝ์Šต๋‹ˆ๋‹ค.
ES6ํด๋ž˜์Šค์˜ ์†์„ฑ๊ณผ ํŒŒ๊ธ‰ ํšจ๊ณผ๋ฅผ ํƒ๊ตฌํ•˜์—ฌ ES6ํด๋ž˜์Šค๋ฅผ ์‹ฌ์ธต์ ์œผ๋กœ ์‚ดํŽด๋ด…์‹œ๋‹ค.

Defining Classes

classํ‚ค์›Œ๋“œ๋Š” ๋‹จ์ง€ ํŠน๋ณ„ํ•œ function์ด๊ณ  ์˜ˆ์ƒ ๋œ ํ•จ์ˆ˜ ํ–‰์œ„๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ด์ „ ๊ฐ์ฒด ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์—์„œ ์ƒˆ๋กœ์šด ES6 ํด๋ž˜์Šค๋กœ ์ „ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์–ด๋ ต์ง€ ์•Š์•„์•ผํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•จ์ˆ˜์ฒ˜๋Ÿผ, class๋Š” ์„ ์–ธ์ด๋‚˜ ํ‘œํ˜„์‹์— ์˜ํ•ด ์ •์˜ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Essential Reading: Learn React from Scratch! (2019 Edition)

Class Declarations

ํด๋ž˜์Šค ์„ ์–ธ์€ classํ‚ค์›Œ๋“œ๋กœ ์ •์˜๋˜๊ณ  ํด๋ž˜์Šค์˜ ์ด๋ฆ„์ด ๋’ค๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

class Animal {}

์šฐ๋ฆฌ๋Š” ์ด๋ฏธ Animal์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ES6๋ฒ„์ „์„ ์ž‘์„ฑํ•  ๋•Œ class์„ ์–ธ์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

class Animal {
  constructor(name, fierce){
    this._name = name;
    this._fierce = fierce;
  }
}

Class Expressions

ํด๋ž˜์Šค ํ‘œํ˜„์‹์€ ์ข€ ๋” ์œ ์—ฐ์„ฑ์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค์˜ ์ด๋ฆ„์ด ์ง€์ •๋˜๊ฑฐ๋‚˜ ์ด๋ฆ„์ด ์ง€์ •๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํด๋ž˜์Šค ํ‘œํ˜„์‹์˜ ์ด๋ฆ„์ด ์ง€์ •๋˜๋ฉด name ์†์„ฑ์€ ํด๋ž˜์Šค ๋ณธ๋ฌธ์˜ ๋กœ์ปฌ ์†์„ฑ์ด๋˜๊ณ  .name์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฆ„์—†๋Š” ํด๋ž˜์Šค ํ‘œํ˜„์‹์€ classํ‚ค์›Œ๋“œ๋ฅผ ๋”ฐ์„œ ์ด๋ฆ„์„ ๊ฑด๋„ˆ๋›ฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 // unnamed
    let animal = class {}

๋ฐ˜๋ฉด์— ๋ช…๋ช… ๋œ ํด๋ž˜์Šค ํ‘œํ˜„์‹์€ ์ด๋ฆ„์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

// named
    let animal = class Animal {}

๊ฐ์ฒด ์ƒ์„ฑ์ž๋ฅผ ES6 ํด๋ž˜์Šค์™€ ๋น„๊ตํ• ๋•Œ ํ˜ธ์ด์ŠคํŠธ๋กœ ์ธํ•ด ๋ฒ”์œ„๊ฐ€ ์ •์˜๋˜๊ธฐ ์ „์— ์•ก์„ธ์Šค ํ•  ์ˆ˜์žˆ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ์ž์™€ ๋‹ฌ๋ฆฌ ํด๋ž˜์Šค๋Š” ํ˜ธ์ด์ŠคํŠธ๊ฐ€ ๋  ์ˆ˜์—†๊ณ  ํ˜ธ์ด์ŠคํŠธ๊ฐ€ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ES6 ํด๋ž˜์Šค์— ํฐ ํ•œ๊ณ„๋กœ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ๋Ÿด ํ•„์š”๋Š” ์—†๋‹ค. ์ข‹์€ ES6์—ฐ์Šต์€ ์–ด๋–ค ๊ธฐ๋Šฅ์ด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค๋ฉด ํ”„๋กœ๊ทธ๋žจ ๋‚ด์˜ ์–ด๋Š ๊ณณ์—์„œ๋‚˜ ์ •์˜๋  ์ˆ˜ ์žˆ์ง€๋งŒ, class์ž์ฒด๊ฐ€ ์ •์˜๋œ ํ›„์—๋งŒ ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•œ๋‹ค๊ณ  ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค.

Constructor and Body of a Class

๋ช…์‹œ๋œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ ํ›„ ์ค‘๊ด„ํ˜ธ{}๋Š” ํด๋ž˜์Šค ๋ณ€์ˆ˜ (์˜ˆ : ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜, ๋ฉ”์†Œ๋“œ ๋˜๋Š” ์ƒ์„ฑ์ž)๋ฅผ ๋ณด์œ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ค‘๊ด„ํ˜ธ ์•ˆ์—์žˆ๋Š” ์ฝ”๋“œ๋Š” ํด๋ž˜์Šค์˜ ๋ณธ๋ฌธ์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋Š” ๋‹จ์ˆœํžˆ ๊ทธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ ๋  ๋•Œ๋งˆ๋‹ค ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž(์ •์˜ ๋œ ๊ณณ)๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์—์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ „์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์„ ๋•Œ ์ˆ˜์‹  ๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜ ๋˜๋Š” ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜๋‚˜์˜ ์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ๊ฐ€ ํด๋ž˜์Šค์™€ ์—ฐ๊ด€ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ ์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š๋„๋ก ์กฐ์‹ฌํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ SyntaxError๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. superํ‚ค์›Œ๋“œ๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž ๋‚ด์—์„œ superclass์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Animal {
  constructor(name, fierce){ // there can only be one constructor method
    this._name = name;
    this._fierce = fierce;
  }
}
ํด๋ž˜์Šค ๋ณธ๋ฌธ ๋‚ด์˜ ์ฝ”๋“œ๋Š” ์—„๊ฒฉ ๋ชจ๋“œ์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

Defining Methods

ํด๋ž˜์Šค์˜ ๋ณธ๋ฌธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์˜ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์˜ ๋™์ž‘์„ ์ •์˜ํ•˜๋Š” ํ”„๋กœํ†  ํƒ€์ž… ๋ฐฉ๋ฒ•์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ES6์ด์ „์— ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๋ฐฉ๋ฒ•์„ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function Animal (name, fierce) {
  Object.defineProperty(this, 'name', {
    get: function() { return name; }
  });

  Object.defineProperty(this, 'fierce', {
    get: function() { return fierce; }
  });
}

Animal.prototype.toString = function() {
  return 'A' + ' ' + ( this.fierce ? 'fierce' : 'tame' ) + ' ' + this.name;
}

ํ˜น์€

function Animal (name, fierce) {
  Object.defineProperty(this, 'name', {
    get: function() { return name; }
  });

  Object.defineProperty(this, 'fierce', {
    get: function() { return fierce; }
  });

  this.toString = function() {
    return 'A' + ' ' + ( this.fierce ? 'fierce' : 'tame' ) + ' ' + this.name;
  }
}

์œ„์— ์ •์˜ํ•œ ๋‘ ๊ฐ€์ง€ ๋ฉ”์†Œ๋“œ๋ฅผ ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์†Œ๋“œ๋ผ๊ณ ํ•˜๋ฉฐ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—์„œ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ES6์—์„œ๋Š” ํ”„๋กœํ† ํƒ€์ž…๊ณผ Static๋ฉ”์†Œ๋“œ์˜ ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์„ ์ •์˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ES6์—์„œ ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์€ ๊ตฌ๋ฌธ์ด ๋” ๊นจ๋—ํ•˜๊ณ (ํ”„๋กœํ†  ํƒ€์ž… ์†์„ฑ์„ ํฌํ•จํ•˜์ง€ ์•Š์Œ) ๋” ์ฝ๊ธฐ ์‰ฝ๋‹ค๋Š” ์ ์„ ์ œ์™ธํ•˜๊ณ ๋Š” ์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

class Animal {

  constructor(name, fierce){
    this._name = name;
    this._fierce = fierce;
  }

  get name() {
    return this._name;
  }

  get fierce() {
    return ` This animal is ${ this._fierce ? 'fierce' : 'tame' }`;
  }

  toString() {
 return `This is a ${ this._fierce ? 'fierce' : 'tame' } ${this._name}`;
  }

}

์—ฌ๊ธฐ์—์„œ๋Š” ๋” ์งง์€ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐœ์˜ getter ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•œ ๋‹ค์Œ ๊ธฐ๋ณธ์ ์œผ๋กœ Animal ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์•ผ์ƒ์ด๊ฑฐ๋‚˜ ๊ธธ๋“ค์ธ ๋™๋ฌผ์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” toString ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” Animalํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํด๋ž˜์Šค ์ž์ฒด์—์„œ๋Š” ํ˜ธ์ถœ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

ES6 ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์†Œ๋“œ๋Š” ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›์•„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๊ฐ์ฒด์ง€ํ–ฅ์  ํ–‰๋™์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํ›„๋“œ์—์„œ๋Š” ์ƒ์† ๊ธฐ๋Šฅ์ด ๊ธฐ์กด ํ”„๋กœํ†  ํƒ€์ž… ์ฒด์ธ์˜ ๊ธฐ๋Šฅ ์ผ ๋ฟ์ด๋ฉฐ ๊ณง ์‚ดํŽด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  `ES6`๋ฉ”์†Œ๋“œ๋Š” ์ƒ์„ฑ์ž๋กœ ์ž‘๋™ ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ `new`ํ‚ค์›Œ๋“œ๋กœ ํ˜ธ์ถœ ๋œ ๊ฒฝ์šฐ `TypeError`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

Static Methods

Static ๋ฉ”์†Œ๋“œ๋Š” ํ˜ธ์ถœ ๋Œ€์ƒ์˜ ํ–‰๋™์„ ์ •์˜ํ•˜์ง€๋งŒ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์— ์˜ํ•ด ํ˜ธ์ถœ ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœํ† ํƒ€์ž… ๋Œ€์‘ ๋ฌผ๊ณผ ๋‹ค๋ฅด๋‹ค๋Š” ์ ์—์„œ ํ”„๋กœํ† ํƒ€์ž… ๋ฐฉ๋ฒ•๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. Static ๋ฉ”์†Œ๋“œ๋Š” ํด๋ž˜์Šค์—์„œ๋งŒ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Static ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋Š” ์‹œ๋„๋Š” ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ํ–‰๋™์œผ๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Static ๋ฉ”์†Œ๋“œ๋Š” static ํ‚ค์›Œ๋“œ๋กœ ์ •์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ Static ๋ฉ”์†Œ๋“œ๋Š” ํด๋ž˜์Šค์˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๋™๋ฌผ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” Animal ํด๋ž˜์Šค์— Static ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ ํ•ด ๋ด…์‹œ๋‹ค :

class Animal {

  constructor(name, fierce){
    this._name = name;
    this._fierce = fierce;
  }

  static animalExamples() {
    return `Some examples of animals are Lion, Elephant, Sheep, Rhinocerus etc`
  }

}

์ด์ œ ํด๋ž˜์Šค ์ž์ฒด์—์„œ animalExamples() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

console.log(Animal.animalExamples()); // "Some examples of animals are Lion, Elephant, Sheep, Rhinocerus etc"

Subclassing in ES6

๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฉ”์†Œ๋“œ์™€ ์†์„ฑ์„ ๊ฐ€์ง„ ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ๊ธฐ๋ณธ ํด๋ž˜์Šค ๋“ฑ์—์„œ ์ด๋Ÿฌํ•œ ์ผ๋ฐ˜์ ์ธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์†ํ•˜๋Š” ๋‹ค๋ฅธ ํŠน์ • ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ES5์—์„œ๋Š” ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋Ÿฌํ•œ ๋™์ž‘์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ–ˆ์œผ๋ฉฐ ๊ตฌ๋ฌธ์€ ๋•Œ๋กœ๋Š” ์ง€์ €๋ถ„ํ•ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ES6๋Š” ์ƒ์†์„ ์‰ฝ๊ฒŒ ํ•˜๋Š” ๋‹ค์†Œ ์ต์ˆ™ํ•œ extendsํ‚ค์›Œ๋“œ๋ฅผ ๋„์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ๋ณธ ํด๋ž˜์Šค์˜ ์†์„ฑ์„ ์‰ฝ๊ฒŒ ์ƒ์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Animal {

  constructor(name, fierce){
    this._name = name;
    this._fierce = fierce;
  }

  get name() {
    return this._name;
  }

  get fierce() {
    return ` This animal is ${ this._fierce ? 'fierce' : 'tame' }`;
  }

  toString() {
     return `This is a ${ this._fierce ? 'fierce' : 'tame' } ${this._name}`;
  }

}


class Felidae extends Animal {

  constructor(name, fierce, family){
    super(name, fierce);
    this._family = family;
  }

  family() {
     return `A ${this._name} is an animal of the ${this._family} subfamily under the ${Felidae.name} family`;
  }
}

์šฐ๋ฆฌ๋Š” Felidae(๋Œ€๊ฐœ ๊ณ ์–‘์ด๋ผ๊ณ  ํ•จ) ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  Animal ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์†ํ•ฉ๋‹ˆ๋‹ค. superํด๋ž˜์Šค(๊ธฐ๋ณธ ํด๋ž˜์Šค) ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด Felidaeํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ์—์„œ superํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Felidaeํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฉ”์†Œ๋“œ์™€ ์ƒ์† ๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ๋ด…์‹œ๋‹ค.

  var Tiger = new Felidae('Tiger', true, 'Pantherinae');

    console.log(Tiger.toString()); // "This is a fierce Tiger"

    console.log(Tiger.family()); // "A Tiger is an animal of the Pantherinae subfamily under the Felidae family"
์ƒ์„ฑ์ž๊ฐ€ ํ•˜์œ„ ํด๋ž˜์Šค ๋‚ด์— ์žˆ์œผ๋ฉด "this"๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— `super()`๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

ํ•จ์ˆ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœํ•˜๋Š” "ํด๋ž˜์Šค"๋ฅผ ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•ด `extends`ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด๋งŒ์œผ๋กœ ์ƒ์„ฑ ๋œ ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.  

์ด ๊ธฐ์‚ฌ์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ์šฐ๋ฆฌ๋Š” ES6์˜ ์ƒˆ๋กœ์šด ํ‚ค์›Œ๋“œ์˜ ๋Œ€๋ถ€๋ถ„์ด ๊ธฐ์กด ํ”„๋กœํ†  ํƒ€์ž… ๊ธฐ๋ฐ˜ ์ƒ์†์— ๋Œ€ํ•œ ๊ตฌ๋ฌธ์  ์„คํƒ•(syntactical sugar)์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ์•˜์Šต๋‹ˆ๋‹ค. ์‹œํŠธ๋ฅผ๋ณด๊ณ  ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

Prototypal Inheritance

์ƒˆ๋กœ์šด 'ES6` ํ‚ค์›Œ๋“œ๋กœ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๊ณ  ์ƒ์†์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์ง€๋งŒ, ํ‘œ์ค€ ๋ ˆ๋ฒจ์—์„œ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค. ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋ฅผ ์‚ดํŽด ๋ด…์‹œ๋‹ค. ๋ชจ๋“  ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋Š” ๋‘ ๋ฒˆ์งธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” private ์†์„ฑ์„ ๊ฐ–์Šต๋‹ˆ๋‹ค (์˜ˆ์™ธ์ ์œผ๋กœ ๋‘ ๋ฒˆ์งธ ๊ฐ์ฒด๋Š” 'null'์„ ๊ฐ€๋ฆฌํ‚ด).์ด ๋‘ ๋ฒˆ์งธ ๊ฐ์ฒด๋Š” 'ํ”„๋กœํ†  ํƒ€์ž…'์ด๋ผ๊ณ ํ•ฉ๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ ๊ฐ์ฒด๋Š”prototype ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์ƒ์† ๋ฐ›๊ณ  ํ”„๋กœํ†  ํƒ€์ž…์€ ์ž์ฒด ํ”„๋กœํ†  ํƒ€์ž…์˜ ์ผ๋ถ€ ์†์„ฑ์„ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ฒด์ธ์˜ ๋งˆ์ง€๋ง‰ ํ”„๋กœํ†  ํƒ€์ž…์˜ ์†์„ฑ์ด'null'์ด ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์† ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์˜ ๊ฐ’์„ ์‹๋ณ„์ž๋กœ ํ• ๋‹นํ•˜์—ฌ ์ƒ์„ฑ ๋œ ๋ชจ๋“  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ์ฒด๋Š” ๋™์ผํ•œ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์—์„œ ๋™์ผํ•œ ํ”„๋กœํ† ํƒ€์ž… ์†์„ฑ์ด ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ํ•ด๋‹น ์†์„ฑ์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ์—์„œ 'Object.prototype`์œผ๋กœ ์ฐธ์กฐ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ๋˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒ์„ฑ ๋œ ๊ฐ์ฒด๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ํ”„๋กœํ† ํƒ€์ž… ์†์„ฑ์—์„œ ํ”„๋กœํ†  ํƒ€์ž…์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, new object()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒˆ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋ฉด ํ•ด๋‹น ๊ฐ์ฒด์˜ ํ”„๋กœํ†  ํƒ€์ž…์€ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์—์„œ ๋งŒ๋“  ๊ฐ์ฒด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Object.prototype์ด๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, new Date()๊ฐ์ฒด๋Š” Date.prototype()๊ณผ Number.prototype()์˜ new Number()๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ํ”„๋กœํ†  ํƒ€์ž… ์ฒด์ธ์˜ ์ƒ๋‹จ์— ์žˆ๋Š” ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด(ํ”„๋กœํ† ํƒ€์ž…)๋กœ๋ถ€ํ„ฐ ์†์„ฑ์„ ์ƒ์†๋ฐ›๋Š” ๊ฒƒ์€ ์ •์ƒ์ด์ง€๋งŒ, Object.prototype์€ ํ”„๋กœํ† ํƒ€์ž…์ด ์—†๊ณ  ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์†์„ฑ(ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ ์ƒ๋‹จ์— ์•‰์Œ)์„ ์ƒ์†๋ฐ›์ง€ ์•Š๋Š” ๋“œ๋ฌธ ํ–‰๋™์„ ๋ณด์ธ๋‹ค.

๊ฑฐ์˜ ๋ชจ๋“  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋‚ด์žฅ ์ƒ์„ฑ์ž๋Š”Object.prototype์—์„œ ์ƒ์†๋˜๋ฏ€๋กœNumber.prototype์€Object.prototype์˜ ์†์„ฑ์„ ์ƒ์†ํ•œ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ด€๊ณ„์˜ ํšจ๊ณผ๋Š” ( 'new Number ()'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ) ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ์—์„œNumber์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉดNumber.prototype๊ณผObject.prototype์—์„œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ƒ์†๋ฐ›์„ ๊ฒƒ์ด๊ณ  ํ”„๋กœํ†  ํƒ€์ž… ์ฒด์ธ์ด๋ฉ๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋Š” ์ •์˜ ๋œ ์†์„ฑ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ด๋Ÿฌํ•œ ์†์„ฑ์„ "์ž์ฒด ์†์„ฑ(own property)"์ด๋ผ๊ณ  ๋ถ€๋ฅด์ง€๋งŒ ์ž์ฒด ์†์„ฑ์—๋งŒ ๊ตญํ•œ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ฐ„์ฃผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์€ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์†์„ฑ์„ ๊ฒ€์ƒ‰ ํ•  ๋•Œ ํฐ ์—ญํ• ์„ํ•ฉ๋‹ˆ๋‹ค.

  • ์ด ์†์„ฑ์€ ๋จผ์ € ๊ฐ์ฒด์—์„œ ์ž์ฒด ์†์„ฑ(own property)์œผ๋กœ ๊ฒ€์ƒ‰๋ฉ๋‹ˆ๋‹ค.
  • ๊ฐ์ฒด์— ์†์„ฑ์ด ์—†์œผ๋ฉด ํ”„๋กœํ† ํƒ€์ž…์ด ๋‹ค์Œ์œผ๋กœ ๊ฒ€์‚ฌ๋ฉ๋‹ˆ๋‹ค.
  • ์†์„ฑ์ด ํ”„๋กœํ† ํƒ€์ž…์— ์—†์œผ๋ฉด ํ”„๋กœํ† ํƒ€์ž…์˜ ํ”„๋กœํ†  ํƒ€์ž…์ด ์ฟผ๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
  • ํ”„๋กœํ† ํƒ€์ž… ์ดํ›„์˜ ํ”„๋กœํ† ํƒ€์ž… ์ฟผ๋ฆฌ๋Š” ์†์„ฑ์ด ๋ฐœ๊ฒฌ๋˜๊ฑฐ๋‚˜ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์˜ ๋์— ๋„๋‹ฌํ•˜์—ฌ ์˜ค๋ฅ˜๊ฐ€ ๋ฐ˜ํ™˜ ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์†๋ฉ๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ”„๋กœํ† ํƒ€์ž… ์ƒ์†์˜ ๋™์ž‘์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์‹œ๋ฎฌ๋ ˆ์ดํŠธํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด ์˜ˆ์ œ์—์„œ๋Š” ES5๋ฉ”์†Œ๋“œ Object.create()๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ •์˜ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

Object.create()๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ํ•ด๋‹น ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.

let Animals = {}; // Animal inherits object methods from Object.prototype.

    Animals.eat = true; // Animal has an own property - eat ( all Animals eat ).

    let Cat = Object.create(Animals); // Cat inherits properties from Animal and Object.prototype.

    Cat.sound = true; // Cat has it's own property - sound ( the animals under the cat family make sounds).

    let Lion = Object.create(Cat); // Lion ( a prestigious cat ) inherits properties from Cat, Animal, and Object.prototype.

    Lion.roar = true; // Lion has its own property - roar ( Lions can raw )

    console.log(Lion.roar); // true - This is an "own property".

    console.log(Lion.sound); // true - Lion inherits sound from the Cat object.

    console.log(Lion.eat); // true - Lion inherits eat from the Animal object.

    console.log(Lion.toString()); // "[object Object]" - Lion inherits toString method from Object.prototype.

์šฐ๋ฆฌ๊ฐ€ ์œ„์—์„œ ํ•œ ๊ฒƒ์— ๋Œ€ํ•œ ๊ตฌ์ˆ ์ ์ธ ํ•ด์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์šฐ๋ฆฌ๋Š”Animals๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ๊ณ Object.prototype์˜ ์†์„ฑ์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ์šฐ๋ฆฌ๋Š” Animals own property์— -eat-์„ true๋กœ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค (๋ชจ๋“  ๋™๋ฌผ์ด ๋จน์Šต๋‹ˆ๋‹ค)
  • Cat์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  Animals์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ์ดˆ๊ธฐํ™” ํ–ˆ์Šต๋‹ˆ๋‹ค.(๊ทธ๋Ÿฌ๋ฏ€๋กœ Cat์€ Animals๊ณผ Object.prototype์˜ ์†์„ฑ์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค)
  • ์šฐ๋ฆฌ๋Š”Cat own property์— -sound-๋ฅผ true๋กœ ์ดˆ๊ธฐํ™” ํ–ˆ์Šต๋‹ˆ๋‹ค (๊ณ ์–‘์ด ๊ฐ€์กฑ ์•„๋ž˜์˜ ๋™๋ฌผ์€ ์†Œ๋ฆฌ๋ฅผ๋ƒ…๋‹ˆ๋‹ค)
  • Lion์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด Cat์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ์ดˆ๊ธฐํ™” ํ–ˆ์Šต๋‹ˆ๋‹ค (๋”ฐ๋ผ์„œ Lion์€Cat,Animal ๋ฐObject.prototype์˜ ์†์„ฑ์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค).
  • ์šฐ๋ฆฌ๋Š” Lion own property์— -roar-์„ true๋กœ ์ดˆ๊ธฐํ™” ํ–ˆ์Šต๋‹ˆ๋‹ค (๋ผ์ด์–ธ์€ ์œผ๋ฅด๋ ํ•ฉ๋‹ˆ๋‹ค.)
  • ๋งˆ์ง€๋ง‰์œผ๋กœ Lion ๊ฐ์ฒด์— ์ž์‹ ์˜ ์†์„ฑ๊ณผ ์ƒ์† ๋œ ์†์„ฑ์„ ๊ธฐ๋กํ–ˆ์œผ๋ฉฐ, Lion ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋จผ์ € ์ฐพ์€ ๋‹ค์Œ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํ”„๋กœํ† ํƒ€์ž…(ํ”„๋กœํ† ํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž…)์œผ๋กœ ์ด๋™ํ•˜์—ฌ ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ–ˆ์œผ๋ฉฐ ์ „์ž์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์„ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ”„๋กœํ† ํƒ€์ž… ์ƒ์†์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ด์ง€๋งŒ ์ •ํ™•ํ•œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์ž…๋‹ˆ๋‹ค.

Summing Up

์ด ๊ธ€์—์„œ ์šฐ๋ฆฌ๋Š” ES6 ํด๋ž˜์Šค์™€ ํ”„๋กœํ† ํƒ€์ž… ์ƒ์†์˜ ๊ธฐ๋ณธ์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ธฐ์‚ฌ๋ฅผ ์ฝ์œผ๋ฉด์„œ ํ•œ๋‘ ๊ฐ€์ง€๋ฅผ ๋ฐฐ์› ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ์ด ์žˆ์œผ๋ฉด ์•„๋ž˜ ๋Œ“๊ธ€ ์„น์…˜์— ๋‚จ๊ฒจ์ฃผ์„ธ์š”.

์ด ๊ธฐ์‚ฌ์ฒ˜๋Ÿผ?? ํŠธ์œ„ํ„ฐ์—์„œ @neoighodaro๋ฅผ ํŒ”๋กœ์šฐํ•˜์„ธ์š”.