๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Frontend/JavaScript

[ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript) ] Prototype๊ณผ prototype property, __proto__ ๊ทธ๋ฆฌ๊ณ  prototype chain

by YWTechIT 2021. 7. 16.
728x90

๐Ÿ“ Class vs ProtoType

JavaScript๋Š” ๊ฐ์ฒด์ง€ํ–ฅํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ง€์› ํ•˜์ง€๋งŒ Class ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋Œ€์‹  Prototype ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค. (ES6 ์ดํ›„์— class ๋ฌธ๋ฒ•์ด ๋“ค์–ด์™”์ง€๋งŒ ์ด๋Š” JS๊ฐ€ class ๊ธฐ๋ฐ˜์˜ ๋ฌธ๋ฒ•์œผ๋กœ ๋ฐ”๋€ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.) ๊ทธ๋ ‡๋‹ค๋ฉด class์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•๊ณผ prototype ๋ฌธ๋ฒ•์€ ์–ธ์ œ ์‚ฌ์šฉํ• ๊นŒ? ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž.

 

// use class
function myInfo() {
    this.name = 'AYW'
    this.age = 27
}

const myName = new myInfo();
const myAge = new myInfo();

console.log(myName.name);    // AYW
console.log(myAge.age);    // 27

// prototype
function myInfo() {}

myInfo.prototype.name = 'AYW';
myInfo.prototype.age = 27;

const myName = new myInfo();
const myAge = new myInfo();

console.log(myName.name);    // AYW
console.log(myAge.age);    // 27

 

์ฐจ์ด์ ์ด ๋Š๊ปด์ง€๋Š”๊ฐ€? class ๋ฌธ๋ฒ•์˜ ๋‹จ์ ์€ myInfo ๋‚ด์— ์žˆ๋Š” ๊ฐ’ ๋ชจ๋‘๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ€๋ น 100๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๋ฉด 200๊ฐœ์˜ ๊ฐ’์ด ๋ฉ”๋ชจ๋ฆฌ ๋ณ€์ˆ˜์— ์˜ฌ๋ผ๊ฐ€์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ, a ๋ณ€์ˆ˜์—๋Š” name๊ณผ age ๋ชจ๋‘๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, b ๋ณ€์ˆ˜์—๋Š” name๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” age ๋ณ€์ˆ˜๊นŒ์ง€ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋œ๋‹ค๋Š” ์ด์•ผ๊ธฐ๋‹ค. ์ด๋Š” ๊ณง ๋ฉ”๋ชจ๋ฆฌ์˜ ํšจ์œจ๊ณผ ๊ด€๋ จ์ด ๋˜๋Š”๋ฐ ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด prototype์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

prototype์€ ํ•จ์ˆ˜๋ฅผ ์ง€์ •ํ•  ๋•Œ prototype์ด๋ผ๋Š” ๊ณ ์œ  ์†์„ฑ ๊ฐ’์„ ์ง€์ •ํ•ด์คŒ์œผ๋กœ์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ๊ฐ์ฒด๋Š” ์ด๋ฏธ ์ง€์ •๋œ prototype์— ์†ํ•œ ํ•จ์ˆ˜ ๊ฐ’๋งŒ ์ฐธ์กฐํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, prototype์„ ์“ฐ๋ฉด 200๊ฐœ์˜ ๋ณ€์ˆ˜๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํ•จ์ˆซ๊ฐ’๋งŒ ์ฐธ์กฐํ•˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ ˆ์•ฝ๋˜๋Š” ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. prototype ์ฝ”๋“œ์˜ ํ๋ฆ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  1. prototype์˜ ๋นˆ ๊ฐ์ฒด๋ฅผ ์–ด๋”˜๊ฐ€์— ์„ ์–ธํ•œ๋‹ค.
  2. myName๊ณผ myAge๋Š” prototype์˜ ๋นˆ ๊ฐ์ฒด ๋‚ด์— ์กด์žฌํ•˜๋Š” Object์˜ ๊ฐ’์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์‰ฝ๊ฒŒ ๋งํ•ด name๊ณผ age๋ฅผ prototype์˜ ๋นˆ ๊ฐ์ฒด ์–ด๋”˜๊ฐ€์— ๋„ฃ์–ด๋‘๊ณ  myName๊ณผ myAge ๋ณ€์ˆ˜๊ฐ€ ํ•ด๋‹น ๊ฐ’์„ ๊บผ๋‚ด์“ฐ๋Š” ํ˜•ํƒœ๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

์ง€๊ธˆ๊นŒ์ง€ ๋‚ด์šฉ์œผ๋กœ ๋ณด์•„ prototype์€ ์ง์ ‘ ํ•จ์ˆ˜๋‚ด๋ถ€์— ์„ ์–ธํ•˜์ง€๋„ ์•Š์€ ๊ฐ’์„ ๋„ฃ์–ด๋‘˜ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ฐฐ์› ๋‹ค. ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์ ‘๊ทผํ•œ๋‹ค๋Š” ๋ง์ธ๊ฐ€? ํ•ด๋‹ต์€ prototype Object ์™€ prototype Link์— ์žˆ๋Š”๋ฐ, ํ•˜๋‚˜์”ฉ ์ฐจ๊ทผ์ฐจ๊ทผ ์‚ดํŽด๋ณด์ž.

728x90

๐Ÿ“ ProtoType Object(constructor), ProtoType Link

๊ฐ์ฒด๋Š” ์–ธ์ œ๋‚˜ ํ•จ์ˆ˜(Function)๋กœ ์ƒ์„ฑ๋œ๋‹ค. ํ•จ์ˆ˜๋กœ ์ •์˜๋˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์–ป๋Š” ์ž๊ฒฉ์ด ์žˆ๋Š”๋ฐ ๋ฐ”๋กœ constructor(์ƒ์„ฑ์ž)๋‹ค. ์ด๊ฒƒ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” new๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

 

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

 

[myInfo ํ•จ์ˆ˜๋Š” constructor(์ƒ์„ฑ์ž)๋ฅผ ๊ฐ–๊ณ ์žˆ๋‹ค]

 

[prototype object์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ชจ์Šต]

 

prototype_object๋Š” constructor(์ƒ์„ฑ์ž)์™€ __proto__ ์†์„ฑ์„ ๊ฐ–๊ฒŒ ๋œ๋‹ค. constructor(์ƒ์„ฑ์ž)๋Š” ์ƒ์„ฑ ํ•จ์ˆ˜์ธ function myInfo() {}๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด __proto__์†์„ฑ์€ ๋ญ˜๊นŒ? ์šฐ๋ฆฌ๊ฐ€ ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ๊ฐ’์„ ์„ ์–ธํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๊ฐ€ ๋ฐ”๋กœ __proto__๋•Œ๋ฌธ์ด๋‹ค. __proto__๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ origin ๊ฒฉ์˜ ํ•จ์ˆ˜์ธ prototype object๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์ฆ‰, name๊ณผ age๋Š” myInfo๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋˜์—ˆ์œผ๋‹ˆ myInfo ํ•จ์ˆ˜์˜ prototype object๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š”๋‹ค. ์ด์ฒ˜๋Ÿผ __proto__ ์†์„ฑ์„ ํ†ตํ•ด ์ƒ์œ„ ํ”„๋กœํ† ํƒ€์ž…๊ณผ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ํ˜•ํƒœ๋ฅผ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ(prototype chain)์ด๋ผ๊ณ  ํ•œ๋‹ค.

// prototype chain1

function myInfo() {}    // ํ•จ์ˆ˜

myInfo.prototype.name = 'AYW'
myInfo.prototype.age = 27

let an = new myInfo();  // ํ•จ์ˆ˜๋ฅผ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ

console.log(an.name) // 'AYW'
console.log(an.age)  // 27

 

 

prototype chain์˜ ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋ฅผ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด์ž.

// prototype chain2

function Boss (){}
Boss.prototype.boolean = true;

function MiddleBoss (){}
MiddleBoss.prototype = new Boss();

function Worker(){}
Worker.prototype = new MiddleBoss();

let o = new Worker();
console.log(o.boolean)    // true

 

์ด ๊ณผ์ •์€ Boss -> MiddleBoss -> Worker ์ˆœ์œผ๋กœ boolean๊ฐ’์„ ๋‚ด๋ ค์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค. ์ด ์ฝ”๋“œ๊ฐ€ ์–ด๋–ป๊ฒŒ o.boolean๊ฐ’์ด true๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์„๊นŒ?

 

  1. ๋งˆ์ง€๋ง‰ ๋ณ€์ˆ˜์ธ o์˜ property ์ค‘ boolean ๊ฐ’์ด ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ณ  ์žˆ์œผ๋ฉด ๋ฐ˜ํ™˜์‹œํ‚จ๋‹ค.
  2. ๊ฐ’์ด ์—†์œผ๋ฉด ์ƒ์„ฑ์ž๋ฅผ ์ถ”์ ํ•œ๋‹ค. ์ƒ์œ„ prototype์ธ Worker์˜ property์—์„œ Worker.prototype.boolean์„ ์ฐพ๊ณ  ์žˆ์œผ๋ฉด ๋ฐ˜ํ™˜์‹œํ‚จ๋‹ค.
  3. ๊ฐ’์ด ์—†์œผ๋ฉด ์ƒ์„ฑ์ž๋ฅผ ์ถ”์ ํ•œ๋‹ค. ์ƒ์œ„ prototype์ธ MiddleBoss์˜ property์—์„œ MiddleBoss.prototype.boolean ์ฐพ๊ณ  ์žˆ์œผ๋ฉด ๋ฐ˜ํ™˜์‹œํ‚จ๋‹ค.
  4. ๊ฐ’์ด ์—†์œผ๋ฉด ์ƒ์„ฑ์ž๋ฅผ ์ถ”์ ํ•œ๋‹ค. ์ƒ์œ„ prototype์ธ Boss์˜ property์—์„œ Boss.prototype.boolean ์ฐพ๊ณ  ์žˆ์œผ๋ฉด ๋ฐ˜ํ™˜์‹œํ‚จ๋‹ค.

 

์ด๋Ÿฐ ์ˆœ์„œ๋กœ ์ง„ํ–‰ ๋  ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ, ๋‹ค์Œ ์ฝ”๋“œ๋Š” ๊ฐ’์ด ์–ด๋–ป๊ฒŒ ๋‚˜์˜ฌ๊นŒ?

// prototype chain3

function Boss (){}
Boss.prototype.boolean = true;

function MiddleBoss (){}
MiddleBoss.prototype = new Boss();

function Worker(){}
let t = new Worker();
t.boolean = false;
Worker.prototype. = t

let o = new Worker();
console.log(o.boolean)    // ??

 

์ •๋‹ต์€ false๋‹ค.  ์™œ ๊ทธ๋Ÿด๊นŒ?

  1. ๋งˆ์ง€๋ง‰ ๋ณ€์ˆ˜์ธ o์˜ property ์ค‘ boolean ๊ฐ’์ด ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ณ  ์žˆ์œผ๋ฉด ๋ฐ˜ํ™˜์‹œํ‚จ๋‹ค.
  2. ๊ฐ’์ด ์—†์œผ๋ฉด ์ƒ์„ฑ์ž๋ฅผ ์ถ”์ ํ•œ๋‹ค. ์ƒ์œ„ Worker.prototype๋ฅผ ์‚ดํ•€๋‹ค.
  3. prototype์— ์ €์žฅ๋˜์–ด์žˆ๋Š” boolean ๊ฐ’์ด 4์ด๊ธฐ ๋•Œ๋ฌธ์— 4๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 


๐Ÿ“Œ reference

  1. javascript-prototype-์ดํ•ดํ•˜๊ธฐ
  2. Javascript ๊ธฐ์ดˆ - Object prototype ์ดํ•ดํ•˜๊ธฐ
  3. ์ƒํ™œ์ฝ”๋”ฉ - prototype
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€