Basic Inheritance with Object.create - Lee-hyuna/33-js-concepts-kr GitHub Wiki

Basic Inheritance with Object.create

λ²ˆμ—­ : http://adripofjavascript.com/blog/drips/basic-inheritance-with-object-create.html

[JavaScript λ‰΄μŠ€ λ ˆν„°] (http://adipofjavascript.com/default.htm)에 처음으둜 κ²Œμ‹œλ˜μ—ˆμŠ΅λ‹ˆλ‹€ .

λͺ‡ 가지 λ¬Έμ œλŠ” μš°λ¦¬κ°€ [μƒμ„±μžλ₯Ό μ‚¬μš©ν•˜μ—¬ κΈ°λ³Έ 상속을 κ΅¬ν˜„ν•˜λŠ” 방법] (http://adipofjavascript.com/blog/drips/basic-inheritance-with-javascript-constructors.html)μ—μ„œ μ‚΄νŽ΄ λ³΄μ•˜μŠ΅λ‹ˆλ‹€. 이번 ν˜Έμ—μ„œλŠ” μƒˆλ‘œμš΄ Object.create λ₯Ό μ‚¬μš©ν•˜μ—¬ λ™μΌν•œ μž‘μ—…μ„ μˆ˜ν–‰ν•˜λŠ” 방법을 μ‚΄νŽ΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

상속을 μœ„ν•΄ μƒμ„±μžλ₯Ό μ‚¬μš©ν•  λ•Œ, μš°λ¦¬λŠ” μƒμ„±μžμ˜prototype 속성에 λ‹€μŒκ³Ό 같이 속성을 μ²¨λΆ€ν•©λ‹ˆλ‹€ :

μ—¬κΈ° μ•½κ°„μ˜ μž¬μΆ©μ „μ΄ μžˆμŠ΅λ‹ˆλ‹€.

function SuperHuman (name, superPower) {
    this.name = name;
    this.superPower = superPower;
}

SuperHuman.prototype.usePower = function () {
    console.log(this.superPower + "!");
};

var banshee = new SuperHuman("Silver Banshee", "sonic wail");

// Outputs: "sonic wail!"
banshee.usePower();

SuperHuman μƒμ„±μžλŠ” μ΄ˆκΈ°ν™” λ‘œμ§μ„ ν¬ν•¨ν•˜κ³ ,SuperHuman.prototype은 λͺ¨λ“ SuperHuman μΈμŠ€ν„΄μŠ€λ“€μ— 걸쳐 κ³΅μœ λ˜λŠ” λ©”μ†Œλ“œλ“€μ„ ν¬ν•¨ν•©λ‹ˆλ‹€.

Object.createλ₯Ό μ‚¬μš©ν•˜μ—¬ 같은 κΈ°λ³Έ λ‘œμ§μ„ κ΅¬ν˜„ν•œλ‹€λ©΄, μ•½κ°„ λ‹€λ₯΄κ²Œ 보일 κ²ƒμž…λ‹ˆλ‹€

var superHuman = {
    usePower: function () {
        console.log(this.superPower + "!");
    }
};

var banshee = Object.create(superHuman, {
    name: { value: "Silver Banshee" },
    superPower: { value: "sonic wail" }
});

// Outputs: "sonic wail!"
banshee.usePower();

이 경우 λ¨Όμ € ν”„λ‘œν†  νƒ€μž… 객체 superHuman 을 μ •μ˜ν•œ λ‹€μŒ Object.create λ₯Ό μ‚¬μš©ν•˜μ—¬ superHuman 을 상속받은 μƒˆλ‘œμš΄ 객체λ₯Ό λ§Œλ“­λ‹ˆλ‹€. 두 번째 μΈμˆ˜λŠ” μ•½κ°„ 이상해 보일지 λͺ¨λ₯΄μ§€λ§Œ κ°„λ‹¨ν•˜κ²Œ 속성 μ„€λͺ…μž 객체 μΌλΏμž…λ‹ˆλ‹€. [Object.defineProperty`λ₯Ό μ‚¬μš©ν•˜μ—¬ 객체의 속성을 λ―Έμ„Έ μ‘°μ •ν•©λ‹ˆλ‹€.] (http://adripofjavascript.com/blog/drips /creating-unwritable-properties-with-object-defineproperty/default.htm).

자, 우리 μžμ‹ μ˜ κΈ°λŠ₯을 μΆ”κ°€ν•˜λŠ” λ™μ•ˆ superHuman μœΌλ‘œλΆ€ν„° 상속받은 μƒˆλ‘œμš΄ νƒ€μž…μ„ λ§Œλ“€κ³  μ‹Άλ‹€λ©΄ μ–΄λ–»κ²Œν•΄μ•Όν• κΉŒμš”? 그게 μ–΄λ–»κ²Œ 생겼어?

var  superHero  =  Object.create(superHuman, {
	allegiance: { value:  'Good' },
	saveTheDay: {
		value:  function() {
		console.log(this.name  +  ' saved the day!')
		},
	},
})

var  marvel  =  Object.create(superHero, {
	name: { value:  'Captain Marvel' },
	superPower: { value:  'magic' },
})
// Outputs: "Captain Marvel saved the day!"

marvel.saveTheDay()

μ—¬νƒœκΉŒμ§€λŠ” κ·ΈλŸ°λŒ€λ‘œ μž˜λλ‹€. κ·ΈλŸ¬λ‚˜ μΊ‘ν‹΄ λ§ˆλΈ” (Captain Marvel)μ€μŠˆνΌ μΈκ°„ν”„λ‘œν†  νƒ€μž… 방법에 μ ‘κ·Ό ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

// Outputs: "magic!"
marvel.usePower();

λ„€, κ·Έλ ‡μŠ΅λ‹ˆλ‹€!

Object.createλ₯Ό μ‚¬μš©ν•˜λ©΄ 객체λ₯Ό ν”„λ‘œν†  νƒ€μž…μœΌλ‘œ μ‚¬μš©ν•  수 있기 λ•Œλ¬Έμ— 상속 체인을 κ°„λ‹¨ν•˜κ²Œ μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜Object.create에 μ˜ν•΄ κ΄€λ¦¬λ˜λŠ” 상속은instanceof에 μ˜ν•΄ 감지 될 수 μ—†μŠ΅λ‹ˆλ‹€. λŒ€μ‹ μ— λ‹€μŒκ³Ό 같이isPrototypeOf λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  ν•„μš”κ°€ μžˆμŠ΅λ‹ˆλ‹€ :

// Outputs: true
console.log(superHero.isPrototypeOf(marvel));

// Outputs: true
console.log(superHuman.isPrototypeOf(marvel));

superHero와superHuman λͺ¨λ‘marvel의 ν”„λ‘œν†  νƒ€μž… 체인의 일뢀이기 λ•Œλ¬Έμ—isPrototypeOf ν˜ΈμΆœμ€ 각각 trueλ₯Ό λ¦¬ν„΄ν•©λ‹ˆλ‹€.

μš°λ¦¬κ°€ κ²€ν†  ν•œ λ‹€λ₯Έ JavaScript κΈ°λŠ₯κ³Ό λ§ˆμ°¬κ°€μ§€λ‘œObject.createλŠ” ECMAScript 5의 κΈ°λŠ₯이며 IE8κ³Ό 같은 κ΅¬ν˜• λΈŒλΌμš°μ €μ—μ„œλŠ” μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

이것이Object.create μ‚¬μš©μ— λŒ€ν•œ κ°„λ‹¨ν•œ μ†Œκ°œμž…λ‹ˆλ‹€. 읽어 μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€!

μ‘°μŠˆμ•„ 클랜튼