Understanding the difference between Object.create() and the new operator. - Lee-hyuna/33-js-concepts-kr GitHub Wiki

Object.create ()와 new μ—°μ‚°μžμ˜ 차이점 이해

κ·Έ 차이λ₯Ό μ•„λŠ” 것이 μ™œ μ€‘μš”ν•œκ°€μš”?

  1. 졜근의 μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ μ•„λ‹Œ μ½”λ“œλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.
  2. ν›Œλ₯­ν•œ κ°œλ°œμžκ°€ 되렀면 λͺ¨λ“  μœ ν˜•μ˜ μ½”λ“œλ₯Ό λ‹€λ£¨λŠ” 방법을 λ°°μ›Œμ•Όν•©λ‹ˆλ‹€. 그것은 였래된 μ½”λ“œλ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€!
  3. ν›„λ“œ μ•„λž˜μ—μ„œ ECMAScript 6에 λ„μž… 된 class ν‚€μ›Œλ“œλŠ” new μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

λ¨Όμ € Object.createκ°€ μˆ˜ν–‰ν•˜λŠ” μž‘μ—…μ„ μ‚΄νŽ΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

var dog = {
    eat: function() {
        console.log(this.eatFood)
    }
};

var maddie = Object.create(dog);
console.log(dog.isPrototypeOf(maddie)); //true
maddie.eatFood = 'NomNomNom'; 
maddie.eat(); //NomNomNom

μœ„μ˜ 예제λ₯Ό λ‹¨κ³„λ³„λ‘œ μ‚΄νŽ΄λ³΄κ³  μ •ν™•νžˆ 무엇이 μΌμ–΄λ‚˜λŠ”μ§€ λ΄…μ‹œλ‹€.

  1. eatμ΄λΌλŠ” ν•˜λ‚˜μ˜ λ©”μ†Œλ“œλ₯Ό 가진 dogλΌλŠ” μ΄λ¦„μ˜ 객체 λ¦¬ν„°λŸ΄μ„ λ§Œλ“­λ‹ˆλ‹€.
  2. ν”„λ‘œν† νƒ€μž…μ„ dog둜 μ„€μ •ν•΄ μ™„μ „νžˆ μƒˆλ‘œμš΄ 객체λ₯Ό μƒμ„±ν•˜λŠ” Object.create(dog)λ₯Ό μ‚¬μš©ν•˜μ—¬ maddieλ₯Ό μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.
  3. dogκ°€ maddie의 ν”„λ‘œν† νƒ€μž…μΈμ§€ ν…ŒμŠ€νŠΈ ν•΄λ³΄μ‹­μ‹œμ˜€.
  4. this.eatFoodλ₯Ό 톡해 좜λ ₯ ν•  λ¬Έμžμ—΄μ„ μ„€μ •ν•˜μ‹­μ‹œμ˜€.
  5. μƒˆλ‘œ 생성 된 객체maddieλ₯Ό μ‚¬μš©ν•˜μ—¬ eatν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜μ‹­μ‹œμ˜€.
  6. μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” ν”„λ‘œν† νƒ€μž… 체인을 ν†΅κ³Όν•˜μ—¬ thisν‚€μ›Œλ“œκ°€ maddie둜 μ„€μ •λœ dog에 eatλ©”μ†Œλ“œλ₯Ό μ°ΎμŠ΅λ‹ˆλ‹€.
  7. NomNomNom을 μ½˜μ†”μ— 좜λ ₯ν•©λ‹ˆλ‹€.

Object.create()λŠ” ν”„λ‘œν† νƒ€μž…μ΄ dog둜 μ„€μ •λœ μ™„μ „νžˆ μƒˆλ‘œμš΄ 객체인 maddieλ₯Ό λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€. μƒˆλ‘­κ²Œ 생성 된 maddieκ°μ²΄λŠ” 이제dog의eat λ©”μ†Œλ“œμ— μ ‘κ·Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

이제 new μ—°μ‚°μžλ₯Ό μ‚΄νŽ΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

var Dog = function(){
    this.eatFood = 'NomNomNom';
    this.eat = function(){
        console.log(this.eatFood)
    }
};

var maddie = new(Dog);
console.log(maddie instanceof Dog); // True
maddie.eat(); //NomNomNom

new μ—°μ‚°μžκ°€ 이 ν•¨μˆ˜μ— μ–΄λ–»κ²Œ μ μš©λ˜λŠ”μ§€, 그리고 λ¬΄μ—‡μ„ν•˜λŠ”μ§€ λ΄…μ‹œλ‹€.

  1. maddieλΌλŠ” μƒˆ 객체λ₯Ό λ§Œλ“­λ‹ˆλ‹€.
  2. maddieλŠ” μƒμ„±μž ν•¨μˆ˜μ˜ ν”„λ‘œν† νƒ€μž…μ„ μƒμ†λ°›μŠ΅λ‹ˆλ‹€.
  3. 1λ²ˆμ—μ„œ 생성 된 object에 thisκ°€ μ„€μ •λœ μƒμ„±μžλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.
  4. 생성 된 객체λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€ (μƒμ„±μžκ°€ 객체λ₯Ό λ°˜ν™˜ν•˜μ§€ μ•Šμ„ λ™μ•ˆ).

Object.create ()와 newμ—°μ‚°μžμ˜ 차이점은 λ¬΄μ—‡μΌκΉŒμš”? 그듀은 λ‘˜ λ‹€ λ˜‘κ°™μ€ κ²ƒμ„ν•˜λŠ” κ²ƒμ²˜λŸΌ λ³΄μž…λ‹ˆλ‹€.
λ‘˜ λ‹€ μƒˆλ‘œμš΄ 객체λ₯Ό λ§Œλ“€κ³  ν”„λ‘œν†  νƒ€μž…μ„ μƒμ†λ°›μŠ΅λ‹ˆλ‹€.

λ°”λΌκ±΄λŒ€. 이 μ˜ˆμ œλŠ” ν˜Όλž€μ„ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€ :

function Dog(){
    this.pupper = 'Pupper';
};

Dog.prototype.pupperino = 'Pups.';
var maddie = new Dog();
var buddy = Object.create(Dog.prototype);

//Using Object.create()
console.log(buddy.pupper); //Output is undefined
console.log(buddy.pupperino); //Output is Pups.

//Using New Keyword
console.log(maddie.pupper); //Output is Pupper
console.log(maddie.pupperino); //Output is Pups.

이 μ˜ˆμ œμ—μ„œ μ£Όλͺ©ν•  핡심 사항은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

console.log(buddy.pupper); //Output is undefined

buddy.pupper의 κ²°κ³ΌλŠ” μ •μ˜λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. Object.create()κ°€ prototype을 Dog둜 μ„€μ •ν•˜λ”λΌλ„ buddyλŠ” μƒμ„±μžμ—μ„œ this.pupper에 μ•‘μ„ΈμŠ€ ν•  수 μ—†μŠ΅λ‹ˆλ‹€.
μ΄λŠ” new Dogκ°€ μ‹€μ œλ‘œ μƒμ„±μž μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λŠ” 반면 Object.createλŠ” μƒμ„±μž μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜μ§€ μ•ŠλŠ” μ€‘μš”ν•œ 차이 λ•Œλ¬Έμž…λ‹ˆλ‹€.

Object.create()와 new μ—°μ‚°μžκ°€ μ–΄λ–»κ²Œ μ„œλ‘œ κ΅¬λ³„λ˜λŠ”μ§€μ— λŒ€ν•œ 톡찰λ ₯을 μ–»κ²Œ λ˜μ—ˆκΈ°λ₯Ό λ°”λžλ‹ˆλ‹€.