Inheritance in JavaScript

midohreeยท2020๋…„ 7์›” 21์ผ
0
post-thumbnail
post-custom-banner

๐Ÿค”...

function Car(name, speed) {
  this.name = name;
  this.speed = speed;
}

Car.prototype.drive = function () {
  console.log(`${this.name} runs at ${this.speed}`)
};

var morning = new Car('morning', 50);

morning.drive();

function Bike(name, speed, maxSpeed) {
  Car.apply(this, arguments)
  this.maxSpeed = maxSpeed;
}

Bike.prototype = Object.create(Car.prototype);
Bike.prototype.constructor = Bike;
Bike.prototype.boost = function () {
  console.log(`${this.name} boosts its speed at ${this.maxSpeed}`)
}

var honda = new Bike('honda', 100, 200);

honda.drive();
honda.boost();

์œ„ ์ฝ”๋“œ์˜ ํ’€์ด๋Š” ๋Œ€๋žต ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • Car.apply(this, arguments); ๋Š” Car์˜ this๋“ค์„ ๊ทธ๋Œ€๋กœ ๋ฐ›์œผ๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. ์ฆ‰, Car ์ƒ์„ฑ์ž์— this์™€ arguments๋ฅผ ์ ์šฉํ•˜๋ผ๋Š” ๋ง์ด๋‹ค. arguments๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

  • Bike.prototype = Object.create(Car.prototype)์€ Car์˜ ๋ฉ”์†Œ๋“œ์ธ drive๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Bike์˜ prototype๊ณผ Car์˜ prototype์„ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

  • Object.create() ๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. Object.create(Car.prototype) ๊ณผ new Car() ์˜ ์ฐจ์ด๋Š”, Object.create๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋˜ ์ƒ์„ฑ์ž๋Š” ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰ ํ”„๋กœํ† ํƒ€์ž…๋งŒ ๋„ฃ๋Š”๋‹ค.

  • Bike์— ๋นˆ Car.prototype์ด ๋“ค์–ด์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์›๋ž˜ ๊ฐ–๊ณ  ์žˆ๋˜ prototype๊ณผ constructor๊ฐ€ ์‚ญ์ œ๋˜์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์‹œ constructor๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ์ž‘์—…์„ ํ•ด ์ค˜์•ผํ•œ๋‹ค.

๋˜ ๋‹ค๋ฅธ ์˜ˆ์ œ๋„ ๋งŒ๋“ค์–ด๋ณผ๊นŒ?

function Fruit () {
  this.colorRed = function () {
    console.log("RED")
  }
}

function Apple (name) {
  this.name = name;
  this.colorRed = function () {
    console.log("RED")
  }
}

var apple = new Apple("apple");

Fruit ํ•จ์ˆ˜์™€ Apple ํ•จ์ˆ˜์— ์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•˜๊ณ ์‹ถ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ? ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” ์ฝ”๋“œ์˜ ์ƒํ•˜์œ„๋ฅผ ๊ตฌ๋ถ„ ํ•ด์•ผํ•œ๋‹ค. Apple๋ณด๋‹ค Fruit๊ฐ€ ์ƒ์œ„์ด๊ธฐ ๋•Œ๋ฌธ์— apple์—์„œ ์ค‘๋ณต๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ง€์›Œ์ค€๋‹ค.

function Fruit () {
  this.colorRed = function () {
    console.log("RED")
  }
}

function Apple (name) {
  โœ… Fruit.call(this);
  this.name = name;
  }
}

var apple = new Apple("apple");

Apple ํ•จ์ˆ˜์—์„œ Fruit์˜ ์†์„ฑ์„ ๊ฐ€์ ธ ์˜ค๊ธฐ ์œ„ํ•ด call ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์ค€๋‹ค. call ๋ฉ”์†Œ๋“œ์˜ ์ธ์ž๋กœ ๋“ค์–ด๊ฐ€๋Š” this๋Š” ๋ญ๊ฐ€ ๋ ๊นŒ?
Fruit ํ•จ์ˆ˜๊ฐ€ call ๋ฉ”์†Œ๋“œ๋กœ ํ˜ธ์ถœ๋˜์–ด Fruit ํ•จ์ˆ˜ ์•ˆ์˜ this๊ฐ€ Apple.call์˜ ์ธ์ž this๊ฐ€ ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ Fruit์™€ Apple์—๊ฒŒ colorRed ๋ผ๋Š” ์†์„ฑ์„ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ์—ˆ๋‹ค.

function Fruit () {
  this.colorRed = function () {
    console.log("RED")
  }
}

โœ…Fruit.prototype.sweet = function () {
  console.log("SWEET")
}

function Apple (name) {
  Fruit.call(this);
  this.name = name;
  }
}

var apple = new Apple("apple");

Fruit์— sweet๋ผ๋Š” ์†์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. Fruit์™€ sweet๋Š” ์—ฐ๊ฒฐ์ด ๋˜์—ˆ์ง€๋งŒ Apple์—๋Š” sweet๊ฐ€ ์—†๋‹ค. ์—ฌ๊ธฐ์„œ Object.create()๊ฐ€ ํ•„์š”ํ•˜๊ฒŒ ๋œ๋‹ค.

Object.create()

์ง€์ •๋œ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด ๋ฐ ์†์„ฑ์„ ๊ฐ–๋Š” ์ƒˆ๋กœ์šด ๋นˆ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Object.create() ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋˜, ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค.

function Fruit () {
  this.colorRed = function () {
    console.log("RED")
  }
}

Fruit.prototype.sweet = function () {
  console.log("SWEET")
}

function Apple (name) {
  Fruit.call(this);
  this.name = name;
  }
}

โœ…Apple.prototype = Object.create(Fruit.prototype);

var apple = new Apple("apple");

Object.create()๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜๋œ ๋นˆ ๊ฐ์ฒด์˜ prototype์ด Fruit.prototype์œผ๋กœ ์„ค์ •๋˜์—ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋˜ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค. ๋ชจ๋“  ํ•จ์ˆ˜์—๋Š” prototype ์†์„ฑ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Apple ๋˜ํ•œ prototype์ด ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋ฅผ Object.create()๋ฅผ ์‚ฌ์šฉํ•ด ๋นˆ ๊ฐ์ฒด๋กœ ์žฌํ• ๋‹น ํ•ด๋ฒ„๋ ธ๋‹ค. prototype์—๋Š” constructor ๋ผ๋Š” ์†์„ฑ์ด ์žˆ๊ณ  ์ด ๋˜ํ•œ ์‚ฌ๋ผ์ ธ๋ฒ„๋ ธ๋‹ค. ๊ทธ๋ž˜์„œ constructor๋ฅผ ๋‹ค์‹œ ์„ค์ • ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

function Fruit () {
  this.colorRed = function () {
    console.log("RED")
  }
}

Fruit.prototype.sweet = function () {
  console.log("SWEET")
}

function Apple (name) {
  Fruit.call(this);
  this.name = name;
  }
}

Apple.prototype = Object.create(Fruit.prototype);
โœ…Apple.prototype.constructor = Apple;

var apple = new Apple("apple");
var food = new Fruit();

food.sweet();
apple.sweet();

์ฐธ์กฐ
MDN : Object.create()
Zerocho blog : ๊ฐ์ฒด ํ™•์žฅ
Poiemaweb : 5.2 ํ”„๋กœํ† ํƒ€์ž… ํŒจํ„ด ์ƒ์†

profile
๐Ÿ’ป
post-custom-banner

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