let's get IT with DAVINA ๐Ÿ’ป

Prototype ๋ณธ๋ฌธ

DEV_IN/JavaScript

Prototype

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 28. 17:24
TypeScript๋ฅผ ๋ฐฐ์šฐ๋ฉด์„œ ๊ฒฐ๊ตญ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ superset์ธ๋ฐ ๊ทธ๋Ÿฌ๋ ค๋ฉด JavaScript ๊ฐœ๋…์„ ์กฐ๊ธˆ ๋” ์ •๋ฆฌ ํ•  ํ•„์š”์„ฑ์„ ๋Š๊ผˆ๋‹ค๋นˆ
Prototype์€ JS์˜ ์ƒ์† ๊ธฐ๋Šฅ, ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค!!!

Prototype

TS๋Š” ์•„๋ž˜์™€๊ฐ™์ด JS์˜ superset์ž…๋‹ˆ๋‹ค. JS์—์„  ์—†์—ˆ๋˜, class, interface, generics, types ๋“ฑ์„ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ๊ฐ€๋Šฅํ–ˆ์Šต๋‹ˆ๋‹ค.

BUT, JS๋„ ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด proto-based๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ์ง€ํ–ฅ์ž…๋‹ˆ๋‹ค. 

๋˜ํ•œ, ES6๋ถ€ํ„ฐ๋Š” class๋ฅผ ์ด์šฉํ•ด์„œ ํ”„๋กœํ† ๋ฅผ ๊ธฐ๋ฐ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

Prototype์€ ์ƒ์†์„ ์œ„ํ•ด ์“ฐ์ด๋Š” ์•„์ด์ž…๋‹ˆ๋‹ค!

์šฐ๋ฆฌ๊ฐ€ Class๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์†์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ JS์—์„  Prototype์„ ์ด์šฉํ•ด์„œ ์ƒ์†์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Prototype์˜ ์˜์–ด ๋œป์€, "์ถ”์ƒํ™”ํ•ด์„œ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋‹ค"๋ž€ ๋Š๋‚Œ์ด๋‹ค. 

So, How to ๊ตฌํ˜„ Proto?

const x = {};
const y = {};
console.log(x);
console.log(y);
console.log(x.toString());
console.log(x.__proto__ === y.__proto__);

const array = [];
console.log(array);

์œ„์™€ ๊ฐ™์ด, ๋นˆ ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์ด๋”๋ผ๋„ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ์ผœ์„œ ํ™•์ธํ•ด๋ณด๋ฉด __Proto__ ์•ˆ์— ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ, array๋”๋ผ๋„ Object Proto๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

function CoffeeMachine(beans) {
  this.beans = beans;
  // Instance member level
  /* this.makeCoffee = shots => {
    console.log('making... โ˜•๏ธ');
  }; */
}

// Prototype member level
CoffeeMachine.prototype.makeCoffee = shots => {
  console.log('making... โ˜•๏ธ');
};
const machine1 = new CoffeeMachine(10);
const machine2 = new CoffeeMachine(20);
console.log(machine1);
console.log(machine2);

function LatteMachine(milk) {
  this.milk = milk;
}
LatteMachine.prototype = Object.create(CoffeeMachine.prototype);

const latteMachine = new LatteMachine(123);
console.log(latteMachine);
latteMachine.makeCoffee();
  • Instance member level์—์„œ๋Š” console.log(machine1)์„ ํ•˜๋ฉด {bean:10}์˜ ๊ฐ์ฒด && makeCoffee๋ผ๋Š” ํ•จ์ˆ˜ ๋‘˜ ๋‹ค ๋ฐ”๋กœ ๋ณด์ธ๋‹ค
  • BUT, prototype member level์ฒ˜๋Ÿผ ๊ตฌํ˜„ํ•˜๋ฉด makeCoffee๊ฐ€ ๋ฐ”๋กœ ๋ณด์ด๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, __Proto__์•ˆ์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.
  • ๋˜ํ•œ, LatteMachine.prototype=Object.create(CoffeeMachine.prototype)์„ ํ†ตํ•ด CoffeeMachine์˜ proto๋ฅผ LatteMachine๋„ ์ƒ์† ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ, Prototype-based Programming์ด๋ž€?

  • JS๊ฐ€ ๊ฐ์ฒด ์ง€ํ–ฅ์„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ• (a style of OOP)
  • behavior reuse ํ–‰๋™์˜ ์žฌ์‚ฌ์šฉ  ์ƒ์†(inheritance)
  • by reusing existing objects
  • that serve as prototype

'DEV_IN > JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Inside JavaScript] Chapter 1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋ณธ ๊ฐœ์š”  (0) 2023.03.27
Module  (2) 2023.02.28
This  (6) 2023.02.28
JavaScript Event Loop๋ž€?  (4) 2023.02.22
priceFormatter  (0) 2023.02.08
Comments