Javascript OOP Inherit

์ƒ์†

  • ECMAScript์—์„œ๋Š” ๊ตฌํ˜„ ์ƒ์†๋งŒ ์ง€์›ํ•˜๋ฉฐ ๊ตฌํ˜„ ์ƒ์†์€ ๋Œ€๊ฐœ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์„ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

ํฌ๋กœํ† ํƒ€์ž… ์ฒด์ธ

  • ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์˜ ๊ธฐ๋ณธ ์•„์ด๋””์–ด๋Š” ํ”„๋กœํ†  ํƒ€์ž… ๊ฐœ๋…์„ ์ด์šฉํ•ด ๋‘ ๊ฐ€์ง€ ์ฐธ์กฐ ํƒ€์ž… ์‚ฌ์ด์—์„œ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์ƒ์„ฑ์ž์—๋Š” ์ƒ์„ฑ์ž ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด(prototype)๊ฐ€ ์žˆ์œผ๋ฉฐ ์ธ์Šคํ„ด์Šค๋Š” ํ”„๋กœํ† ํƒ€์ž…์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋‚ด๋ถ€ ํฌ์ธํ„ฐ(__proto__)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. - ๊ทธ๋ ‡๋‹ค๋ฉด ํ”„๋กœํ† ํƒ€์ž…์ด ์‚ฌ์‹ค ๋‹ค๋ฅธ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๋ผ๋ฉด ์–ด๋–จ๊นŒ์š”? ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ํ”„๋กœํ† ํƒ€์ž…(A) ์ž์ฒด์— ๋‹ค๋ฅธ ํ”„๋กœํ† ํƒ€์ž…(B)์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋ฉฐ, B์—๋Š” ๋˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๊ฐ€ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค.
function SuperType() {
  this.property = true;
}

SuperType.prototype.getSuperValue = function() {
  return this.property;
};

function SubType() {
  this.subproperty = false;
}

// inherit from SuperType
SubType.prototype = new SuperType();

// ์ƒˆ ํ”„๋กœํ† ํƒ€์ž…์€ SuperType์˜ ์ธ์Šคํ„ด์Šค์ด๋ฏ€๋กœ SuperType ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐ€์งˆ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ ์™ธ์— SuperType์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
SubType.prototype.getSubValue = function() {
  return this.subproperty;
};

var instance = new SubType();
console.log(instance.getSuperValue()); //true
  • SubType์˜ ๊ธฐ๋ณธ ํ”„๋กœํ† ํƒ€์ž… ๋Œ€์‹  ์ƒˆ ํ”„๋กœํ† ํƒ€์ž…(new SuperType())์ด ํ• ๋‹น๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  • ์ƒˆ ํ”„๋กœํ† ํƒ€์ž…์€ SuperType์˜ ์ธ์Šคํ„ด์Šค์ด๋ฏ€๋กœ SuperType ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐ€์งˆ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ ์™ธ์— SuperType์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  • ์ฆ‰, instance ๋ณ€์ˆ˜๋Š” SubType.prototype์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  Subtype.prototype์€ Super.prototype์„ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.
  • getSuperValue() ๋ฉ”์„œ๋“œ๋Š” SuperType.prototype ๊ฐ์ฒด์— ๋‚จ์ง€๋งŒ property๋Š” SubType.prototype์— ์กด์žฌํ•จ์„ ๋ˆˆ์—ฌ๊ฒจ ๋ณด์‹ญ์‹œ์˜ค.
  • ์ด๋ ‡๊ฒŒ ๋˜๋Š” ๊ฒƒ์€ getSuperValue()์ด ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ์ด๋ฉฐ property๋Š” ์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. SubType.prototype์€ SuperType์˜ ์ธ์Šคํ„ด์Šค ์ด๋ฏ€๋กœ property๋Š” SubType.prototype์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
  • SubType.prototype์˜ constructor ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ฎ์–ด์ผ์œผ๋ฏ€๋กœ instance.construcor๊ฐ€ SuperType์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ ๋„ ์‚ดํŽด๋ณด์‹ญ์‹œ์˜ค.
  • instance.getSuperValue()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด 3๋‹จ๊ณ„๋กœ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. 1) ์ธ์Šคํ„ด์Šค์—์„œ, 2) SubType.prototype์—์„œ 3) SuperType.prototype์—์„œ ๊ฒ€์ƒ‰ํ•˜๋ฉฐ 3๋‹จ๊ณ„์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ณธ ํ”„๋กœํ† ํƒ€์ž…

  • ๋ชจ๋“  ์ฐธ์กฐ ํƒ€์ž…์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ”„๋กœํ† ํƒ€์ž… ์ฒด๋‹๋ฅด ํ†ตํ•ด Object๋ฅผ ์ƒ์†ํ•ฉ๋‹ˆ๋‹ค.
  • ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ํ”„๋กœํ† ํƒ€์ž…์€ Object์˜ ์ธ์Šคํ„ด์Šค์ด๋ฏ€๋กœ ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€ ํ”„๋กœํ† ํƒ€์ž… ํฌ์ธํ„ฐ๋Š” Object.prototype์„ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.
function Supertype() {}
function Object() {}

Object.prototype.constructor = Object;
Supertype.prototype.__proto__ === Object.prototype;

ํ”„๋กœํ† ํƒ€์ž…๊ณผ ์ธ์Šคํ„ด์Šค ์‚ฌ์ด์˜ ๊ด€๊ณ„

  • ํ”„๋กœํ† ํƒ€์ž…๊ณผ ์ธ์Šคํ„ด์Šค ์‚ฌ์ด์˜ ๊ด€๊ณ„๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    • ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ instanceof ์—ฐ์‚ฐ์ž ์ž…๋‹ˆ๋‹ค. instanceof ์—ฐ์‚ฐ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์ž๊ฐ€ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์— ์กด์žฌํ• ๋•Œ true๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
console.log(instance instanceof Object); //true
console.log(instance instanceof SuperType); //true
console.log(instance instanceof SubType); //true
  • ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ isPrototypeOf() ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์ฒด์ธ์— ์กด์žฌํ•˜๋Š” ๊ฐ ํ”„๋กœํ† ํƒ€์ž…์€ ๋ชจ๋‘ isPrototypeOf() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฒด์ธ์— ์กด์žฌํ•˜๋Š” ์ธ์Šคํ„ด์Šค์—์„œ true๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
console.log(Object.prototype.isPrototypeOf(instance)); //true
console.log(SuperType.prototype.isPrototypeOf(instance)); //true
console.log(SubType.prototype.isPrototypeOf(instance)); //true

๋ฉ”์„œ๋“œ

  • ํ•˜์œ„ํƒ€์ž…(subtype)์—์„œ ์ƒ์œ„ ํƒ€์ž…(supertype)์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ(๋ฉ”์„œ๋“œ ๋ฎ์–ด์“ฐ๊ธฐ)ํ•˜๊ฑฐ๋‚˜ ์ƒ์œ„ ํƒ€์ž…์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ํ”„๋กœํ† ํƒ€์ž…์ด ํ• ๋‹น๋œ ๋‹ค์Œ ํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํ”„๋กœํ† ํƒ€์ž…์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์•„๋ž˜ ์˜ˆ์ œ์—์„œ ์ค‘์š”ํ•œ ์ ์€ ๋‘ ๋ฉ”์„œ๋“œ๊ฐ€ ๋ชจ๋‘ ํ”„๋กœํ† ํƒ€์ž…์ด SuperType์˜ ์ธ์Šคํ„ด์Šค๋กœ ํ• ๋‹น๋œ ๋‹ค์Œ ์ •์˜๋˜์—ˆ๋‹ค๋Š” ์ .
function SuperType() {
  this.property = true;
}

SuperType.prototype.getSuperValue = function() {
  return this.property;
};

function SubType() {
  this.subproperty = false;
}

//inherit from SuperType
SubType.prototype = new SuperType();

//new method
SubType.prototype.getSubValue = function() {
  return this.subproperty;
};

//override existing method
SubType.prototype.getSuperValue = function() {
  return false;
};

var instance = new SubType();
console.log(instance.getSuperValue()); //false
  • ์ฒซ๋ฒˆ์งธ ๋ฉ”์„œ๋“œ์ธ getSubValue()๋Š” SubType์— ์ถ”๊ฐ€ํ•œ ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ๋‘๋ฒˆ์งธ ๋ฉ”์„œ๋“œ์ธ getSuperVlaue()๋Š” ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์— ์ด๋ฏธ ์กด์žฌํ•˜์ง€๋งŒ instance์—์„œ ๊ธฐ์กด ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€๋ ธ์Šต๋‹ˆ๋‹ค.
  • ์ค‘์š”ํ•œ์ ์€ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ์จ์„œ ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค๋ฉด ์ฒด์ธ์„ ๋ฎ์–ด์“ฐ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋˜๋ฏ€๋กœ ํ”„๋กœํ† ํƒ€์ž…๊ณผ ์ฒด์ธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.
function SuperType() {
  this.property = true;
}
SuperType.prototype.getSuperValue = function() {
  return this.property;
};
function SubType() {
  this.subproperty = false;
}
//inherit from SuperType
SubType.prototype = new SuperType();

// ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ์‹œ๋„ ์•ž์ค„์„ ๋ฌดํšจํ™” ํ•ฉ๋‹ˆ๋‹ค.
SubType.prototype = {
  getSubValue: function() {
    return this.subproperty;
  },
  someOtherMethod: function() {
    return false;
  },
};
var instance = new SubType();
console.log(instance.getSuperValue()); //error!
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ด์ œ ํ”„๋กœํ† ํƒ€์ž…์—๋Š” SuperType์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹ˆ๋ผ Object์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋“ค์–ด์žˆ์œผ๋ฏ€๋กœ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์ด ๋Š์–ด์ ธ์„œ SubType๊ณผ SuperType ์‚ฌ์ด์—๋Š” ์•„๋ฌด ๊ด€๊ณ„๋„ ์—†์Šต๋‹ˆ๋‹ค.

ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์˜ ๋ฌธ์ œ

  • ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์€ ๊ฐ•๋ ฅํ•œ ์ƒ์† ๋ฐฉ๋ฒ•์ด์ง€๋งŒ ๋ฌธ์ œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์š”๋ฌธ์ œ๋Š” ์ฐธ์กฐ๊ฐ’์„ ํฌํ•จํ•œ ํ”„๋กœํ† ํƒ€์ž…๊ณผ ๊ด€๋ จ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ”„๋กœํ† ํƒ€์ž… ํ”„๋กœํผํ‹ฐ์— ๋“ค์–ด์žˆ๋Š” ์ฐธ์กฐ ๊ฐ’์ด ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ ๊ณต์œ ๋œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ƒ๊ธฐํ•˜์‹ญ์‹œ์˜ค.
  • ์ด ๋•Œ๋ฌธ์— ํ”„๋กœํผํ‹ฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ”„๋กœํ† ํƒ€์ž… ๋Œ€์‹  ์ƒ์„ฑ์ž์— ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์ƒ์†์„ ๊ตฌํ˜„ํ•˜๋ฉด ํ”„๋กœํ† ํƒ€์ž…์ด ๋‹ค๋ฅธ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜๋ฏ€๋กœ ์ฒ˜์Œ์— ์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ์˜€๋˜ ๊ฒƒ๋“ค์ด ํ”„๋กœํ† ํƒ€์ž… ํ”„๋กœํผํ‹ฐ๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค. (์ฆ‰, ๊ณต์œ ๋ฅผ ์•ˆํ•˜๋ ค๊ณ  ํ•œ ์†์„ฑ(ํ”„๋กœํผํ‹ฐ)๋“ค์ด ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์„ ์ด์šฉํ•จ์œผ๋กœ์จ ๊ณต์œ ํ•˜๊ฒŒ ๋œ๋‹ค. )
function SuperType() {
  this.colors = ["red", "blue", "green"];
}
// ๊ณต์œ ๋ฅผ ์•ˆํ•˜๋ ค๊ณ  ๋งŒ๋“ค์—ˆ๋˜ color๋ฐฐ์—ด..
function SubType() {}

//inherit from SuperType
SubType.prototype = new SuperType();
// ์„œ๋ธŒํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž…์— ์ƒˆ๋กœ์šด ์ƒ์„ฑ์ž ์ •์˜
// ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ  SuperType()์— ์žˆ๋˜ color๊ฐ’์€ ์ƒˆ๋กœ์šด ๊ฐ์ฒด ์•ˆ์— ์†ํ•˜๊ฒŒ ๋˜๊ณ 
// ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ SubType ํ”„๋กœํ† ํƒ€์ž…์— ๋ฐ˜ํ™˜.
// SubType ํ”„๋กœํ† ํƒ€์ž…์€ SubType์˜ ์ธ์Šคํ„ด์Šค์— ๋ชจ๋‘ ๊ณต์œ ํ•˜๋Š” ์„ฑ์งˆ.
// ์ฆ‰, ๊ณต์œ ๋ฅผ ์•ˆํ•˜๋ ค๊ณ  ๋งŒ๋“  color๋ฐฐ์—ด์„ SubType์ธ์Šคํ„ด์Šค์— ๋ชจ๋‘ ๊ณต์œ ํ•˜๋Š” ์ƒํ™ฉ์ด ์ƒ๊น€

var instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors); //"red,blue,green,black"

var instance2 = new SubType();
console.log(instance2.colors); //"red,blue,green,black"
  • ๊ฒฐ๊ตญ SubType์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ color ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ณต์œ ํ•˜๊ฒŒ ๋˜์–ด instance1.colors๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด instance2.colors ์—๋„ ๋ฐ˜์˜์ด ๋ฉ๋‹ˆ๋‹ค. - ๋‘๋ฒˆ์งธ ๋ฌธ์ œ๋Š” ํ•˜์œ„ ํƒ€์ž… ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ ์ƒ์œ„ ํƒ€์ž…์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋ฐฉ๋ฒ•์€ ์—†๋‹ค.

์ƒ์„ฑ์ž ํ›”์น˜๊ธฐ

  • ๊ธฐ๋ณธ ์•„์ด๋””์–ด๋Š” ๋งค์šฐ ๋‹จ์ˆœํ•ฉ๋‹ˆ๋‹ค. ํ•˜์œ„ํƒ€์ž… ์ƒ์„ฑ์ž ์•ˆ์—์„œ ์ƒ์œ„ ํƒ€์ž… ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋Š” ๋‹จ์ˆœํžˆ ์ฝ”๋“œ๋ฅผ ํŠน์ • ์ปจํ…์ŠคํŠธ์—์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฐ์ฒด์ผ ๋ฟ์ž„์„ ์—ผ๋‘์— ๋‘๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ๊ฐ์ฒด์—์„œ apply()์™€ call() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ์ž๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ์ด ์ดํ•ด๋ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
function SuperType() {
  this.colors = ["red", "blue", "green"];
}

function SubType() {
  //inherit from SuperType
  SuperType.call(this);
}

var instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors); //"red,blue,green,black"

var instance2 = new SubType();
console.log(instance2.colors); //"red,blue,green"
  • ์ด ์ฝ”๋“œ๋Š” SuperType() ํ•จ์ˆ˜์— ๋“ค์–ด์žˆ๋Š” ๊ฐ์ฒด ์ดˆ๊ธฐํ™” ์ฝ”๋“œ ์ „์ฒด๋ฅผ SubType ๊ฐ์ฒด์—์„œ ์‹คํ–‰ํ•˜๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ์ž์‹ ๋งŒ์˜ colors ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ

  • ์ƒ์„ฑ์ž ํ›”์น˜๊ธฐ ํŒจํ„ด์€ ํ•˜์œ„ ํƒ€์ž…์˜ ์ƒ์„ฑ์ž ์•ˆ์—์„œ ์ƒ์œ„ ํƒ€์ž…์˜ ์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋Š” ์ƒ์„ฑ์ž ํ›”์น˜๊ธฐ ํŒจํ„ด์ด ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ๋ณด๋‹ค ๋‚˜์€์  ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
function SuperType(name) {
  this.name = name;
}

function SubType() {
  //inherit from SuperType passing in an argument
  SuperType.call(this, "Nicholas");

  //instance property
  this.age = 29;
}

var instance = new SubType();
console.log(instance.name); //"Nicholas";
console.log(instance.age); //29
  • ์ด ์ฝ”๋“œ์—์„œ SuperType ์ƒ์„ฑ์ž๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ name ํ•˜๋‚˜๋ฅผ ๋ฐ›๊ณ  ๋‹จ์ˆœํžˆ ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜๊ธฐ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. SubType์ƒ์„ฑ์ž ๋‚ด์—์„œ SuperType ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ• ๋•Œ ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Š” SubType์ธ์Šคํ„ด์Šค์˜ name ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์ƒ์„ฑ์ž ํ›”์น˜๊ธฐ ํŒจํ„ด์˜ ๋ฌธ์ œ

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

์กฐํ•ฉ์ƒ์†

  • ์กฐํ•ฉ์ƒ์†์€ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ๊ณผ ์ƒ์„ฑ์ž ํ›”์น˜๊ธฐ ํŒจํ„ด์„ ์กฐํ•ฉํ•ด ๋‘ ํŒจํ„ด์˜ ์žฅ์ ๋งŒ์„ ์ทจํ•˜๋ ค๋Š” ์ ‘๊ทผ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์•„์ด๋””์–ด๋Š” ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์„ ์จ์„œ ํ”„๋กœํ† ํƒ€์ž…์— ์กด์žฌํ•˜๋Š” ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†ํ•˜๊ณ  ์ƒ์„ฑ์ž ํ›”์น˜๊ธฐ ํŒจํ„ด์œผ๋กœ ์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ๋ฅผ ์ƒ์†ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ”„๋กœํ† ํƒ€์ž…์— ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•ด์„œ ํ•จ์ˆ˜๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ๊ฐ ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ณ ์œ ํ•œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
function SuperType(name) {
  this.name = name;
  this.colors = ["red", "blue", "green"];
}

SuperType.prototype.sayName = function() {
  console.log(this.name);
};

function SubType(name, age) {
  SuperType.call(this, name);
  this.age = age;
}

SubType.prototype = new SuperType();
SubType.prototype.sayAge = function() {
  console.log(this.age);
};

var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
console.log(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"Nicholas";
instance1.sayAge(); //29

var instance2 = new SubType("Greg", 27);
console.log(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg";
instance2.sayAge(); //27
  • ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ๊ณผ ์ƒ์„ฑ์ž ํ›”์น˜๊ธฐ ํŒจํ„ด์˜ ๋‹จ์ ์„ ๋ชจ๋‘ ํ•ด๊ฒฐํ•œ ์กฐํ•จ์ƒ์†์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๊ฐ€์žฅ ์ž์ฃผ ์“ฐ์ด๋Š” ์ƒ์† ํŒจํ„ด์ž…๋‹ˆ๋‹ค. ์กฐํ•ฉ์ƒ์†์€ instanceOf()์™€ isPrototypeOf()์—์„œ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœํ† ํƒ€์ž… ์ƒ์†

  • ์ด ๋ฐฉ๋ฒ•์€ ํ”„๋กœํ† ํƒ€์ž…์„ ์จ์„œ ์ƒˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋ฐ˜๋“œ์‹œ ์ปค์Šคํ…€ ํƒ€์ž…์„ ์ •์˜ํ•  ํ•„์š”๋Š” ์—†๋‹ค๋Š” ๋ฐ์„œ ์ถœ๋ฐœํ•ฉ๋‹ˆ๋‹ค. ํฌ๋กํฌ๋“œ๊ฐ€ ์†Œ๊ฐœํ•œ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
function object(o) {
  function F() {}
  F.prototype = o;
  return new F();
}
  • object() ํ•จ์ˆ˜๋Š” ์ž„์‹œ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์–ด์ง„ ๊ฐ์ฒด(๋งค๊ฐœ๋ณ€์ˆ˜ o)๋ฅผ ์ƒ์„ฑ์ž์˜ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ํ• ๋‹นํ•œ ๋‹ค์Œ ์ž„์‹œ ์ƒ์„ฑ์ž์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด object()๋Š” ์ฃผ์–ด์ง„ ๊ฐ์ฒด์˜ ์‚ฌ๋ณธ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
var person = {
  name: "Nicholas",
  friends: ["Shelby", "Court", "Van"],
};

var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");

var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");

console.log(person.friends); // "Shelby,Court,Van,Rob,Barbie"
  • ์ด ๋ฐฉ๋ฒ•์—์„œ๋Š” ์ผ๋‹จ ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ๊ธฐ๋ฐ˜์ด ๋  ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ธฐ๋ฐ˜ ๊ฐ์ฒด๋ฅผ object()์— ๋„˜๊ธด๋‹ค์Œ ๊ฒฐ๊ณผ ๊ฐ์ฒด๋ฅผ ์ ์ ˆํžˆ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” person ๊ฐ์ฒด๊ฐ€ ๊ธฐ๋ฐ˜ ๊ฐ์ฒด์ด๋ฏ€๋กœ object()ํ•จ์ˆ˜์— ๋„˜๊ฒจ์„œ ์ƒˆ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์ƒˆ ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…์€ person์ž…๋‹ˆ๋‹ค.
  • ECMAScript 5ํŒ์€ ํ”„๋กœํ† ํƒ€์ž… ์ƒ์†์˜ ๊ฐœ๋…์„ ๊ณต์‹์ ์œผ๋กœ ์ˆ˜์šฉํ•˜์—ฌ Object.create() ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋‘๊ฐœ ๋ฐ›๋Š”๋ฐ ํ•˜๋‚˜๋Š” ๋‹ค๋ฅธ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…์ด ๋  ๊ฐ์ฒด์ด๋ฉฐ, ์˜ต์…˜์ธ ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ์ƒˆ ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•  ํ”„๋กœํผํ‹ฐ๋ฅผ ๋‹ด์€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ์“ฐ๋ฉด Object.creat()๋Š” object() ๋ฉ”์„œ๋“œ์™€ ๋˜‘๊ฐ™์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
var person = {
  name: "Nicholas",
  friends: ["Shelby", "Court", "Van"],
};

var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");

var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");

console.log(person.friends); // "Shelby,Court,Van,Rob,Barbie"
  • Object.create()์˜ ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” Object.defineProperties() ์˜ ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๊ฐ™์€ ํ˜•์‹์ž…๋‹ˆ๋‹ค. ์ฆ‰ ์ถ”๊ฐ€ํ•  ํ”„๋กœํผํ‹ฐ๋งˆ๋‹ค ์„œ์ˆ ์ž์™€ ํ•จ๊ป˜ ์ •์˜ํ•˜๋Š” ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ์‹์œผ๋กœ ์ถ”๊ฐ€ํ•œ ํ”„๋กœํผํ‹ฐ๋Š” ๋ชจ๋‘ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด์— ์žˆ๋Š” ๊ฐ™์€ ์ด๋ฆ„์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€๋ฆฝ๋‹ˆ๋‹ค.
var person = {
  name: "Nicholas",
  friends: ["Shelby", "Court", "Van"],
};

var anotherPerson = Object.create(person, {
  name: {
    value: "Greg",
  },
});

console.log(anotherPerson.name); // "Greg"
  • ํ”„๋กœํ† ํƒ€์ž… ์ƒ์†์œผ๋กœ ๋งŒ๋“  ๊ฐ์ฒด๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ํ•˜์ง€๋งŒ ์ƒ์„ฑ์ž๋ฅผ ๋‹ค ๋”ฐ๋กœ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†๋‹ค๋Š” ์ ์—์„œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ํ”„๋กœํ† ํƒ€์ž… ํŒจํ„ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฐธ์กฐ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๋“ค์€ ๋ชจ๋‘ ๊ทธ ๊ฐ’์„ ๊ณต์œ ํ•จ์„ ์œ ๋…ํ•˜์‹ญ์‹œ์˜ค.

๊ธฐ์ƒ ์ƒ์†

  • ๊ธฐ์ƒ์ƒ์†์ด๋ž€ ๊ฐœ๋…์€ ํ”„๋กœํ† ํƒ€์ž… ์ƒ์†๊ณผ ๋ฐ€์ ‘ํžˆ ์—ฐ๊ด€๋œ, ์ธ๊ธฐ์žˆ๋Š” ํŒจํ„ด์ด๋‹ค.
  • ๊ธฐ๋ณธ ์•„์ด๋””์–ด๋Š” ๊ธฐ์ƒ ์ƒ์„ฑ์ž๋‚˜ ํŒฉํ„ฐ๋ฆฌ ํŒจํ„ด๊ณผ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์ƒ์†์„ ๋‹ด๋‹นํ•  ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , ์–ด๋–ค ์‹์œผ๋กœ๋“  ๊ฐ์ฒด๋ฅผ ํ™•์žฅํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ธฐ์ƒ ์ƒ์† ํŒจํ„ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
function createAnother(original) {
  var clone = object(original);

  clone.sayHi = function() {
    alert("hi");
  };

  return clone;
}
  • ๊ธฐ๋ฐ˜ ๊ฐ์ฒด original์„ object() ํ•จ์ˆ˜์— ๋„˜๊ธด ๊ฒฐ๊ณผ๋ฅผ clone์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ clone ๊ฐ์ฒด์— sayHi()๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
var person = {
  name: "Nicholas",
  friend: ["Shelby", "Court", "Van"],
};

var anotherPerson = creatAnother(person);
anotherPerson.sayHi(); // "hi"
  • ๊ธฐ์ƒ ์ƒ์† ํŒจํ„ด์€ ๊ฐ์ฒด๋ฅผ ์ฃผ๋กœ ๊ณ ๋ คํ•  ๋•Œ ์‚ฌ์šฉํ•  ํŒจํ„ด์ด์ง€ ์ปค์Šคํ…€ ํƒ€์ž…๊ณผ ์ƒ์„ฑ์ž์— ์–ด์šธ๋ฆฌ๋Š” ํŒจํ„ด์€ ์•„๋‹™๋‹ˆ๋‹ค.
  • ๊ธฐ์ƒ ์ƒ์†์— ๊ผญ object() ๋ฉ”์„œ๋“œ๊ฐ€ ํ•„์š”ํ•œ ๊ฑด ์•„๋‹™๋‹ˆ๋‹ค. ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋ชจ๋‘ ์ด ํŒจํ„ด์— ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ธฐ์ƒ ์กฐํ•ฉ ์ƒ์†

  • ์กฐํ•ฉ ์ƒ์†์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๊ฐ€์žฅ ์ž์ฃผ ์“ฐ์ด๋Š” ์ƒ์† ํŒจํ„ด์ด์ง€๋งŒ ๋น„ํšจ์œจ์ ์ธ ๋ฉด๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด ํŒจํ„ด์—์„œ ๊ฐ€์žฅ ๋น„ํšจ์œจ์ ์ธ ๋ถ€๋ถ„์€ ์ƒ์œ„ ํƒ€์ž… ์ƒ์„ฑ์ž๊ฐ€ ํ•ญ์ƒ ๋‘ ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์€ ํ•˜์œ„ ํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด, ๋‹ค๋ฅธ ํ•œ๋ฒˆ์€ ํ•˜์œ„ ํƒ€์ž… ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ ์ž…๋‹ˆ๋‹ค.
  • ํ•˜์œ„ ํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž…์€ ์ƒ์œ„ ํƒ€์ž…๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ชจ๋‘ ์ƒ์†ํ•˜๋Š”๋ฐ, ํ•˜์œ„ ํƒ€์ž…์ƒ์„ฑ์ž๊ฐ€ ์‹คํ–‰๋˜๋Š” ์ˆœ๊ฐ„ ์ด๋ฅผ ๋ชจ๋‘ ๋ฎ์–ด์“ฐ๋ฏ€๋กœ ๋ณ„ ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
// ์กฐํ•ฉ ์ƒ์† ์˜ˆ์ œ
function SuperType(name) {
  this.name = name;
  this.colors = ["red", "blue", "green"];
}

SuperType.prototype.sayName = function() {
  console.log(this.name);
};

function SubType(name, age) {
  SuperType.call(this, name); // SuperType()์„ ๋‘ ๋ฒˆ์งธ ํ˜ธ์ถœ (1)
  this.age = age;
}

SubType.prototpye = new SuperType(); //SuperType()์„ ์ฒ˜์Œ ํ˜ธ์ถœ
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function() {
  alert(this.age);
};
  • (1) ํ‘œ์‹œํ•œ ๋ถ€๋ถ„์€ SuperType ์ƒ์„ฑ์ž๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ์ด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋ฉด SubTpye.prototype ์—๋Š” name๊ณผ colors ๋‘ ๊ฐ€์ง€ ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋‚จ์Šต๋‹ˆ๋‹ค. ์ด ๋‘˜์€ SuperType์˜ ์ธ์Šคํ„ด์Šค ํฌ๋กœํผํ‹ฐ์˜€์ง€๋งŒ ์ด์ œ๋Š” SubType์˜ ํ”„๋กœํ† ํƒ€์ž…์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. SubType ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด SuperType ์ƒ์„ฑ์ž ์—ญ์‹œ ํ˜ธ์ถœ๋˜๋ฉด์„œ ์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ name๊ณผ color๋ฅผ ์ƒˆ ๊ฐ์ฒด์— ๋งŒ๋“œ๋Š”๋ฐ ์ด๋“ค์€ ํ”„๋กœํ† ํƒ€์ž…์— ์กด์žฌํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€๋ฆฝ๋‹ˆ๋‹ค.
  • ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ์ ์ธ ์•„์ด๋””์–ด๋Š” ํ•˜์œ„ํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด ์ƒ์œ„ ํƒ€์ž…์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ํ•„์š”ํ•œ๊ฑด ์ƒ์œ„ ํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž… ๋ฟ์ž…๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•ด ๊ธฐ์ƒ ์ƒ์†์„ ์จ์„œ ์ƒ์œ„ ํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์†ํ•œ ๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ ํ•˜์œ„ ํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž…์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์ƒ ์กฐํ•ฉ ์ƒ์†์˜ ๊ธฐ๋ณธ ํŒจํ„ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
function inheritPrototype(subType, superType) {
  var prototype = object(superType.prototype); //create object
  prototype.constructor = subType; //augment object
  subType.prototype = prototype; //assign object
}
  • ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ผ์–ด๋‚˜๋Š” ์ฒซ ๋‹จ๊ณ„๋Š” ์ƒ์œ„ ํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ๋ณต์ œํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค.
  • ๋‹ค์Œ์—๋Š” constructor ํ”„๋กœํผํ‹ฐ๋ฅผ prototype์— ํ• ๋‹นํ•ด์„œ ํ”„๋กœํ† ํƒ€์ž…์„ ๋ฎ์–ด์“ธ ๋•Œ ๊ธฐ๋ณธ constructor ํ”„๋กœํผํ‹ฐ๊ฐ€ ์‚ฌ๋ผ์ง€๋Š” ํ˜„์ƒ์— ๋Œ€๋น„ํ•ฉ๋‹ˆ๋‹ค.
  • ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•˜์œ„ ํƒ€์ž…์˜ ํ”„๋กœํ† ํƒ€์ž…์— ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
function object(o) {
  function F() {}
  F.prototype = o;
  return new F();
}

function inheritPrototype(subType, superType) {
  var prototype = object(superType.prototype); //create object
  prototype.constructor = subType; //augment object
  subType.prototype = prototype; //assign object
}

function SuperType(name) {
  this.name = name;
  this.colors = ["red", "blue", "green"];
}

SuperType.prototype.sayName = function() {
  console.log(this.name);
};

function SubType(name, age) {
  SuperType.call(this, name);
  this.age = age;
}

inheritPrototype(SubType, SuperType);

SubType.prototype.sayAge = function() {
  console.log(this.age);
};

var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
console.log(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"Nicholas";
instance1.sayAge(); //29

var instance2 = new SubType("Greg", 27);
console.log(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg";
instance2.sayAge(); //27
  • ํ•ด๋‹น ์ฐธ์กฐ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐ€์ ธ์•ผํ•  ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด SuperType.prototype ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กญ๊ฒŒ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ subType.prototype์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
  • ์ธ์Šคํ„ด์Šค ํ”„๋กœํ† ํƒ€์ž…์—๋Š” SuperType.prototype์„ ๋„ฃ์–ด์„œ ๊ฒฐ๊ตญ subType.prototype์ด SuperType.prototype์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋งŒ๋“œ๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค.
  • ์ด ์˜ˆ์ œ๋Š” SuperType ์ƒ์„ฑ์ž๋ฅผ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ SubType.prototype ์— ๋ถˆํ•„์š”ํ•˜๊ณ  ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š”์ ์—์„œ ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค. ๊ธฐ์ƒ ์กฐํ•ฉ ์ƒ์†์€ ์ฐธ์กฐ ํƒ€์ž…์—์„œ ๊ฐ€์žฅ ํšจ์œจ์ ์ธ ์ƒ์† ํŒจ๋Ÿฌ๋‹ค์ž„์œผ๋กœ ํ‰๊ฐ€ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์ด ์˜จ์ „ํžˆ ์œ ์ง€๋˜๋ฏ€๋กœ instanceof ์™€ isPrototypeOf() ๋ฉ”์„œ๋“œ๋„ ์ •์ƒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
ยฉ 2021 Merlin.ho, Built with Gatsby