Prototype in Javascript - Lee-hyuna/33-js-concepts-kr GitHub Wiki


๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ํ•จ์ˆ˜๋Š” EMPTY(๊ธฐ๋ณธ๊ฐ’)์ธ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์†์„ฑ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ฑฐ๊ธฐ์— ํŠน์„ฑ๊ณผ ๋ฐฉ๋ฒ•์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.


Note: ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜์ด๋‹ค.


ํ•จ์ˆ˜ x๊ฐ€ ์žˆ๊ณ  x1์ด x์˜ prototype์„ ์ด์–ด๋ฐ›์œผ๋ฏ€๋กœ x์˜ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•˜์ž.


var x = function(j) {
  this.i = 0;
  this.j = j;
  
  this.GetJ =  function() {
  	return this.j;
  }
}

var x1 = new x(2);

console.log(x1); 
  // > x {i: 0, j: 2, GetJ: ฦ’}
 
console.log(x1.GetJ());
  // > 2

Note: ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ๊ด€ํ•œ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ํฌํ•จ๋œ ํ•จ์ˆ˜๋ฅผ ์ง€์นญํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•ด ์ƒ์„ฑ๋  ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.


์ด์ œ ์›ํ•˜๋Š” ๊ฐ์ฒด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉฐ x์—์„œ ์ƒ์„ฑ๋œ ๋ชจ๋“  ๊ฐ์ฒด์—๋Š” GetJ() ๋ฉ”์„œ๋“œ๊ฐ€ ๋ถ€์ฐฉ๋˜์–ด ์žˆ์œผ๋ฉฐ, GetJ() ๋ฉ”์„œ๋“œ๋Š” ๋ถˆ๋Ÿ‰ ์ƒํƒœ์ด๋‹ค. ์ด์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์€ ํ”„๋กœํ† ํƒ€์ž…์ด๋‹ค. GetJ() ๋ฐฉ๋ฒ•์„ x์˜ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ๊ฐ€์ ธ๊ฐ„๋‹ค.


var x = function(j) {
  this.i = 0;
  this.j = j;
}

x.prototype.GetJ =  function() {
  	return this.j;
 }

var x1 = new x(2);

console.log(x1); 
  // > x {i: 0, j: 2}
 
console.log(x1.GetJ());
  // > 2

ํ•˜์ง€๋งŒ x1์—๋Š” GetJ()์ด ์—†๋‹ค๋Š” ๊ฒƒ์ด ์œ ์ผํ•œ ์ฐจ์ด์  ์ด์ „๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•˜๋ฏ€๋กœ ์—ฌ์ „ํžˆ ์ •ํ™•ํ•œ ์ถœ๋ ฅ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์„ ์ฐพ์•„๋ณด๊ณ (๋ถ€๋ชจ ๋ถ€๋ชจ๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ€์„œ!) GetJ()์„ ์ฐพ๋Š”๋‹ค.


Note: Prototype Chain Javascript์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์€ ๋งˆ์Šคํ„ฐ ๊ฐ์ฒด์—์„œ ์ƒ์„ฑ๋œ๋‹ค.


์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•  ์ ์€ x -> function -> object ๋”ฐ๋ผ์„œ x์—์„œ ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ํ˜ธ์ถœํ•˜๋ฉด, ๊ทธ perticularํ•จ์ˆ˜์— x๊ฐ€ ์žˆ์ง€ ์•Š์œผ๋ฉด, ๋‚˜์ค‘์— ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์œ„์•„๋ž˜๋กœ ํ‘œ์‹œ๋œ๋‹ค. ํ•จ์ˆ˜์— prototype chain์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, undefined ์ƒํƒœ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด,


var x = function() {
}

console.dir(x);


๊ทธ๋ž˜์„œ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ ์ „์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ˜ธ์ถœ์„ ํ•˜๊ฒŒ ๋˜๋ฉด


console.log(x.toString());
// > function() {
// >
// >  }

์—ฌ๊ธฐ์„œ x๋Š” toString()์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์ง€๋งŒ ์œ„๋กœ ์˜ฌ๋ผ๊ฐ€๋ณด๋ฉด prototype chain์— ์žˆ๋‹ค.



Prototypes in Depth

Javascipt๋Š” Java์™€ ๊ฐ™์€ Class defination์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๋Š”๋‹ค. ์—ฌ๊ธฐ์„œ Base class์—์„œ ์„œ๋ธŒ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•œ๋‹ค.


//Baseclass
var Bookshop = function() {
  this.cost = 100;
}

Bookshop.prototype.bill = function() {
  console.log('Your Bill is: $'+ this.cost);
}

Bookshop์€ Base Class๋กœ bill์ด prototype์œผ๋กœ ์†ํ•ด์žˆ๋‹ค.


//Subclass
 var BuyBook = function(title,cost) {
  Bookshop.call(this); // Statement 1
  
  this.title = title;
  if(cost) 
    this.cost = cost ;
 }

BuyBook.prototype = Object.create(Bookshop.prototype); // Statement 2
BuyBook.prototype.constructor = BuyBook; // Statement 3

Bookshop์€ Base Class๋กœ BuyBook์€ ๊ทธ์˜ Sub Class๋กœ ์†ํ•ด์žˆ๋‹ค.



Explaination

  1. Bookshop.call(this)

์ด ๋ผ์ธ์€ ์ƒ์„ฑ์ž๋ฅผ callํ•  ๋•Œ vlaue๊ฐ’์œผ๋กœ this๋ฅผ ๋„˜๊ฒจ์ฃผ์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ `BuyBook : this = { this.cost = 100; }๊ฐ€ ๋œ๋‹ค.

  1. BuyBook.prototype = Object.create(Bookshop.prototype);

์ด ๋ผ์ธ์€ Bookshop.prototype์˜ ์ดˆ๊ธฐ๊ฐ’์„ ์…‹ํŒ…ํ•ด์ค€๋‹ค.

  1. BuyBook.prototype.constructor = BuyBook;

์ด ๋ผ์ธ์€ BuyBook ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์…‹ํŒ…ํ•ด์ค€๋‹ค. (BuyBook์€ ๋ฉ”์†Œ๋“œ์ด๋‹ค)


 var person_1 = new BuyBook('GOT', 200);
 var person_2 = new BuyBook('Harry Potter');

 person_1.bill();
 //> Your Bill is: $200
 person_2.bill();
 //> Your Bill is: $100

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ƒ์†์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. BuyBook.protype์—์„œ ์ƒˆ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜์—ฌ bill() ๋ฉ”์„œ๋“œ๋ฅผ ๋ฎ์–ด์“ธ ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค๋ฉด BuyBook.prototype.bill = function() { } ์ด๋ ‡๊ฒŒ!


๐Ÿ˜„

โš ๏ธ **GitHub.com Fallback** โš ๏ธ