Object.prototype.__proto__
ì§ìì´ ì¤ë¨ëììµëë¤: ì´ ê¸°ë¥ì ë ì´ì ê¶ì¥ëì§ ììµëë¤. ì¼ë¶ ë¸ë¼ì°ì ììë ìì§ ì§ìí ì ìì§ë§ ê´ë ¨ ì¹ íì¤ìì ì´ë¯¸ ì ê±°ëìê±°ë ì ê±° ê³¼ì ì ìë ê²½ì°ê° ìì¼ë©°, í¸íì±ì ìí´ ì ì§ëê³ ìì ì ììµëë¤. ì¬ì©ì í¼íê³ ê°ë¥íë¤ë©´ 기존 ì½ë를 ì ë°ì´í¸íì¸ì. ê²°ì ì ì§ìí íë¨ì í¸íì± í를 íì¸íì¸ì. ì´ ê¸°ë¥ì ì¸ì ë ì§ ìëì ì¤ë¨í ì ììì ì ìíì¸ì.
주ì: ê°ì²´ì
[[Prototype]]ì ë³ê²½íë ê²ì ìµì JavaScript ìì§ì´ ìì± ì ê·¼ì ìµì ííë ë°©ìì í¹ì±ì 모ë ë¸ë¼ì°ì ë° JavaScript ìì§ìì ë§¤ì° ë린 ìì ì ëë¤. ìì 구조를 ë³ê²½íë ê²ì´ ì±ë¥ì 미ì¹ë ìí¥ì 미ë¬íê³ ê´ë²ìíë©°,obj.__proto__ = ...문ì ììëë ìê° ë¿ë§ ìëë¼[[Prototype]]ì´ ë³ê²½ë ê°ì²´ì ì ê·¼í ì ìë 모ë ì½ëë¤ì ëí´ìë ìí¥ì ì¤ ì ììµëë¤. ì±ë¥ì ê´ì¬ì´ ìë¤ë©´ ê°ì²´ì[[Prototype]]ì¤ì ì í¼í´ì¼ í©ëë¤. ëìObject.create()를 ì¬ì©íì¬ ìíë[[Prototype]]ì¼ë¡ ì ê°ì²´ë¥¼ ë§ëì¸ì.
주ì:
Object.prototype.__proto__ë ì¤ëë ëë¶ë¶ì ë¸ë¼ì°ì ìì ì§ìëì§ë§, ê·¸ ì¡´ì¬ì ì íí ëìì ì¤ì§ ì¹ ë¸ë¼ì°ì ìì í¸íì±ì ë³´ì¥í기 ìí ë ê±°ì 기ë¥ì¼ë¡ì ECMAScript 2015 ì¬ììì ë¹ë¡ì íì¤íëììµëë¤. ë ëì ì§ìì ìí´ ëìObject.getPrototypeOf()를 ì¬ì©íì¸ì.
Objectì __proto__ ìì±ì ì ê·¼íê³ ì íë ê°ì²´ì ë´ë¶ ìì±ì¸ [[Prototype]](ê°ì²´ ëë null)를 ë
¸ì¶íë ì ê·¼ì ìì±(getter ë° setter í¨ì)ì
ëë¤.
__proto__ì ì¬ì©ì ë
¼ëì ì¬ì§ê° ìì¼ë©° ê¶ì¥íì§ ììµëë¤. ìëë ECMAScript ì¸ì´ ì¬ìì í¬í¨ëì§ ììììë ë¶êµ¬íê³ ìµì ë¸ë¼ì°ì ììë ì´ë¥¼ 구ííìµëë¤. ìµê·¼ììì¼ ECMAScript 2015 ì¬ììì ì¹ ë¸ë¼ì°ì ìì í¸íì±ì ìí´ íì¤íëìì¼ë¯ë¡ í¥íìë ì§ìì ë ê²ì
ëë¤. ê·¸ë¬ë __proto__ë ëì´ì ì¬ì©íì§ ì길 ë°ë¼ë©°, ëì Object.getPrototypeOf / Reflect.getPrototypeOf ë° Object.setPrototypeOf / Reflect.setPrototypeOf를 ê¶ì¥í©ëë¤(ê°ì²´ì [[Prototype]] ì¤ì ì ì±ë¥ì ê³ ë ¤í ê²½ì° í¼í´ì¼ íë ë린 ìì
ì
ëë¤).
ê°ì²´ 리í°ë´ì ì ìí ë Object.create() ëì __proto__ ìì±ì ì¬ì©í¨ì¼ë¡ì¨ ê°ì²´ì [[Prototype]]ì ì¤ì í ìë ììµëë¤. ì°¸ê³ : object initializer / literal syntax.
ì¤ëª
__proto__ getter í¨ìë ê°ì²´ì ë´ë¶ [[Prototype]] ê°ì ë
¸ì¶í©ëë¤. ê°ì²´ 리í°ë´ì ì¬ì©íì¬ ìì±ë ê°ì²´ì ê²½ì° ì´ ê°ì Objectì
ëë¤. ë°°ì´ ë¦¬í°ë´ì ì¬ì©íì¬ ìì±ë ê°ì²´ì ê²½ì° ì´ ê°ì Arrayì
ëë¤. í¨ìì ê²½ì° ì´ ê°ì Functionì
ëë¤. new funì ì¬ì©íì¬ ìì±ë ê°ì²´ì ê²½ì° (ì¬ê¸°ì funì JavaScriptìì ì ê³µíë ë´ì¥ ìì±ì í¨ì ì¤ íëì
ëë¤. Array, Boolean, Date, Number, Object, String ë± - JavaScriptê° ë°ì í¨ì ë°ë¼ ì¶ê°ë ìë¡ì´ ìì±ì í¬í¨) ì´ ê°ì íì fun.prototypeì
ëë¤. new funì ì¬ì©íì¬ ìì±ë ê°ì²´ì ê²½ì° funì ì¤í¬ë¦½í¸ì ì ìë í¨ìì´ë©° ì´ ê°ì fun.prototypeì ê°ì
ëë¤. (ì¦, ìì±ìê° ë¤ë¥¸ ê°ì²´ë¥¼ ëª
ìì ì¼ë¡ ë°ííì§ ììê±°ë ì¸ì¤í´ì¤ê° ìì±ë ì´íì fun.prototypeì´ ë¤ì í ë¹ë ê²½ì°ì
ëë¤.)
__proto__ setter를 ì¬ì©íë©´ ê°ì²´ì [[Prototype]]ì´ ë³ê²½ë ì ììµëë¤. ê°ì²´ë Object.isExtensible()ì ë°ë¼ íì¥ ê°ë¥í´ì¼ í©ëë¤. ê·¸ë ì§ ìì¼ë©´ TypeErrorê° ë°ìí©ëë¤. ì ê³µë ê°ì ê°ì²´ ëë nullì¬ì¼ í©ëë¤. ë¤ë¥¸ ê°ì ì ê³µíë©´ ì무 í¨ê³¼ê° ììµëë¤.
íë¡í íì ì´ ììì ì¬ì©ëë ë°©ìì ì´í´íë ¤ë©´ ê°ì´ë 문ì ììê³¼ íë¡í íì ì²´ì¸ë¥¼ 참조íì¸ì.
__proto__ ìì±ì getter ë° setter í¨ìë¡ êµ¬ì±ë Objectì ê°ë¨í ì ê·¼ì ìì±ì
ëë¤. Object를 참조íë ê²½ì° __proto__ì ì ê·¼íë©´ Object를 ì°¾ì§ë§, Object를 참조íì§ ìë ê²½ì°ìë ì°¾ì ì ììµëë¤. Objectë³´ë¤ ë¨¼ì ë¤ë¥¸ __proto__ ìì±ì´ ë°ê²¬ëë ê²½ì° ì´ ìì±ì ìí´ Objectë ì¨ê²¨ì§ëë¤.
ìì
>__proto__ ì¬ì©í기
function Circle() {}
const shape = {};
const circle = new Circle();
// Set the object prototype
// DEPRECATED. ììì©ì¼ ë¿ì
ëë¤. ì¤ì ì½ëììë ì´ë ê² íì§ ë§ì¸ì.
shape.__proto__ = circle;
// Get the object prototype
console.log(shape.__proto__ === Circle); // false
const ShapeA = function () {};
const ShapeB = {
a() {
console.log("aaa");
},
};
console.log((ShapeA.prototype.__proto__ = ShapeB));
const shapea = new ShapeA();
shapea.a(); // aaa
console.log(ShapeA.prototype === shapea.__proto__); // true
// ëë
const ShapeC = function () {};
const ShapeD = {
a() {
console.log("a");
},
};
const shapeC = new ShapeC();
shapeC.__proto__ = ShapeD;
shapeC.a(); // a
console.log(ShapeC.prototype === shapeC.__proto__); // false
// ëë
function Test() {}
Test.prototype.myname = function () {
console.log("myname");
};
const a = new Test();
console.log(a.__proto__ === Test.prototype); // true
a.myname(); // myname
// ëë
const fn = function () {};
fn.prototype.myname = function () {
console.log("myname");
};
var obj = {
__proto__: fn.prototype,
};
obj.myname(); // myname
ëª ì¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification > # sec-object.prototype.__proto__ > |