[JavaScript] Prototype & Class

Hannahhhยท2022๋…„ 7์›” 22์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
29/47

๐Ÿ” Prototype

JavaScript๋Š” prototype ๊ธฐ๋ฐ˜ ์–ธ์–ด๋กœ, ๋ชจ๋“  ๊ฐ์ฒด๋“ค์ด ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ๋“ค์„ ์ƒ์† ๋ฐ›๊ธฐ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ์œผ๋กœ์จ, ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๊ฐ–๋Š”๋‹ค.

  • prototpye์€ ์›ํ˜• ๊ฐ์ฒด(original form)๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋Š” ์ƒ์œ„ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ , ๊ทธ ์ƒ์œ„ ํ”„๋กœํ† ํƒ€์ž…๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค. ์ด๋ฅผ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ(prototype chain)์ด๋ผ๊ณ  ํ•˜๋ฉฐ,๋‹ค๋ฅธ ๊ฐ์ฒด์— ์ •์˜๋œ ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ์„ ํ•œ ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. -MDN


class Human{
  constructor(name, age){
    this.name = name;
    this.age = age;
  }
  sleep(){
    console.log(`${this.name}์€ ์ž ์— ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.`);
  }
}

let kimcoding = new Human('๊น€์ฝ”๋”ฉ', 30);

// .prototype ์„ ์‚ฌ์šฉํ•ด prototype ์†์„ฑ์„ ์ง์ ‘ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
Human.prototype.constructor === Human; // true

// .__proto__ ์†์„ฑ์„ ์‚ฌ์šฉํ•ด kimcoding์˜ prototype ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
Human.prototype === kimcoding.__proto__; // true

Human.prototype.sleep === kimcoding.sleep; // true

// instance์—์„œ constructor์— ์ ‘๊ทผํ•  ๋•Œ๋„ .constructor ์‚ฌ์šฉ์„ ์‚ฌ์šฉํ•ด ์ ‘๊ทผ ๊ฐ€๋Šฅ
kimcoding.constructor; // ฦ’ Human()
kimcoding.constructor.name; // 'Human' 
Human.prototype.constructor === kimcoding.constructor // true

this.

new ํ‚ค์›Œ๋“œ๋กœ instance๋ฅผ ์ƒ์„ฑํ–ˆ์„ ๋•Œ, ํ•ด๋‹น Instance๊ฐ€ this์˜ ๊ฐ’์ด๋œ๋‹ค.

.prototype

์ƒ์„ฑ์ž ํ•จ์ˆ˜(constructor)๊ฐ€ .prototype ์†์„ฑ์„ ์ด์šฉํ•ด ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด(์›ํ˜• ๊ฐ์ฒด)๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ

์œ„์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด,
Human.prototype.sleep === kimcoding.sleep; // true
Human class์˜ sleep ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœํ† ํƒ€์ž…์— ์žˆ์œผ๋ฉฐ, Human class์˜ instance์ธ kimcoding์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

.__proto__

.__proto__ ์†์„ฑ์„ ์ด์šฉํ•ด ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ด, ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์ž์‹ ์˜ ํ”„๋กœํ† ํƒ€์ž…์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค(๋งํฌ).

  • ๋ชจ๋“  ๊ฐ์ฒด(instance)๊ฐ€ ์‚ฌ์šฉ

์œ„์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด,
Human.prototype === kimcoding.__proto__; // true
Human class์˜ ํ”„๋กœํ† ํƒ€์ž…์€ Human class์˜ instance์ธ kimcoding์˜ __proto__์™€ ๊ฐ™๋‹ค.

.constuctor

.constructor ์†์„ฑ์„ ์ด์šฉํ•ด ์›๋ณธ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด,
Human.prototype.constructor === kimcoding.constructor // true
Human class์˜ ํ”„๋กœํ† ํƒ€์ž…์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” Human class๋กœ, Human class์˜ instance์ธ kimcoding์˜ ์›๋ณธ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.




๐Ÿ‘€ Class , Instance, Prototype์˜ ๊ด€๊ณ„

์œ„์˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  kimcoding์„ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

Human {
  name: '๊น€์ฝ”๋”ฉ',
  age: 30,
  __proto__: { 
  constructor: ฦ’ Human(), 
  sleep: ฦ’ sleep() 
  }
}

kimcoding์˜ __proto__๋Š” Human.prototype์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
Human.prototype์˜ constructor๋Š” Human class๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ , Human์˜ prototype์€ Human.prototype์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.



โœ” Array(๋ฐฐ์—ด) Class, Instance, Prototype์˜ ๊ด€๊ณ„

๋งํฌํ…์ŠคํŠธ




Reference:
์ฝ”๋“œ์Šคํ…Œ์ด์ธ 
MDN: https://developer.mozilla.org/ko/docs/Learn/JavaScript/Objects/Object_prototypes

http://speakingjs.com/es5/ch17.html

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