ãJavaScriptã«ã¯ãã¯ã©ã¹ã¨ãã®ç¶æ¿ã¯ãªããããããã¿ã¤ããã¼ã¹ã®ç¶æ¿ããµãã¼ããã¦ãããã¨ãã¾ã¼ãããã¯ããè¨ããã決ã¾ãæå¥ã§ãããããããJavaScriptã«ãããã¢ã¬ãç¶æ¿ã¨å¼ãã§ãããã®ãã©ããï¼ ãç¶æ¿ãã¨ããã°ãå¤ãã®äººãé常ã®ç¶æ¿ãã¤ã¾ãã¯ã©ã¹ãã¼ã¹ã®ç¶æ¿ãã¤ã¡ã¼ã¸ããã§ãããããç¡æèã«é常ã®ç¶æ¿ã¨ã®ã¢ããã¸ã¼ã追ããã¨ã«ãªãã§ããããããã¯å±éºã ã¨æãã¾ããã
ããããã¿ã¤ããã¨ããç¶æ¿ãã¨ããè¨èããé£æ³ãã常èçãªã¤ã¡ã¼ã¸ã¯å ¨é¨æ¨ã¦ã¦ãäºå®ãç½ç´ããçºãã¾ãããããã®äºå®ã¨ã¯ããJavaScriptã«ã¯ãå®è¡æããããã£æ¤ç´¢ã®ã¡ã«ããºã ããããã¨ãããã ãã§ãã
ä»åã®å
容ï¼
- ããããã¿ã¤ããããããªãã®ãã¨ã ãããããªã
- __proto__ããããã£ã__proto__ãªãã¸ã§ã¯ãã__proto__ãã§ã¼ã³
- __proto__ãã§ã¼ã³ã追ãããã¦ã¿ã
- __proto__ãã§ã¼ã³ã使ã£ãããããã£æ¤ç´¢
- ãã®ç¶ãã¨ä»åã®ã¾ã¨ã
âããããã¿ã¤ããããããªãã®ãã¨ã ãããããªã
ããããã¿ã¤ããã¨ããè¨èããããã¶ã¤ã³ãã¿ã¼ã³ã®Prototypeãã¿ã¼ã³ã¨ããSelfã®ãããªã¯ã©ã¹ã¬ã¹è¨èªã®ã¯ãã¼ãã³ã°ï¼ãªãã¸ã§ã¯ãè¤è£½ï¼ãé£æ³ãã人ãããã§ããããããããããªãã¸ã§ã¯ãã®çæã«é¢ããæ¦å¿µã§ããããJavaScriptã§ã¯ãã¼ãã³ã°ã使ã£ã¦ããããã§ã¯ãªãã®ã§ããã®æå³ã§ã®ããããã¿ã¤ããã¯å¿ãã¾ãããï¼
ã§ã¯ãJavaScriptã®ãããã¿ã¤ãã§ãã ⦠â¦ãããããããããJavaScriptã®è©±é¡ã«éå®ãã¦ããããããã¿ã¤ããã£ã¦è¨èã使ããã¨èªä½ãã¤ããã®ã ããã
JavaScriptã«ãããããããã¿ã¤ãã»ãªãã¸ã§ã¯ããã£ã¦è¨èã¯ãã¾ã£ããç°ãªã2ã¤ã®æå³ã§ä½¿ããã¦ãã¾ããæ··ä¹±ãé¿ããããã«ãããã§ã¯ãã__proto__ãªãã¸ã§ã¯ããã¨ãprototypeãªãã¸ã§ã¯ããã¨ããè¨èã使ãåãã¾ãããã®2ã¤ã¯å¥ç©ã§ãï¼ï¼å¾ã§éãã説æããï¼
ã¾ããå®æãªã¢ããã¸ã¼ãããç¶æ¿ãã¨è¨ãã®ãããã¾ãï¼è¦åºãã¯ä¾å¤ï¼ã代ããã«ãããããã£æ¤ç´¢ãï¼property lookup; property searchï¼ã使ãã¾ããJavaScriptã®ããããã£æ¤ç´¢ã®ã¡ã«ããºã ãç¥ã£ãä¸ã§ããããã©ããã¦ããç¶æ¿ãã¨å¼ã³ãããªããããã¯ã¾ã¼åæã§ããã©ã
â__proto__ããããã£ã__proto__ãªãã¸ã§ã¯ãã__proto__ãã§ã¼ã³
JavaScriptã®å ¨ã¦ã®ãªãã¸ã§ã¯ãã«ã__proto__ã¨ããï¼ãªãã ãPythonã£ã½ãï¼ååã®ããããã£ãå¿ ãããã¾ãããã ãããã®__proto__ããããã£ãå ¬éããã¦ãªãå¦çç³»ãããã®ã§æ³¨æã以ä¸ã§ã¯ã__proto__ããããã£ãã¦ã¼ã¶ã¼ï¼JavaScriptããã°ã©ãï¼ããè¦ãã¦è§¦ããåæã§è©±ããã¾ãã
ãªãã¸ã§ã¯ãxã®__proto__ããããã£ã¯æ¬¡ã®2ã¤ã®æ§è³ªãæã¡ã¾ãã
- ãã®å¤ã¯ãnullã§ããããã¾ãã¯xã¨å¥ãªãªãã¸ã§ã¯ãã§ããã
- xããã¯ããã¦ãx.__proto__ãx.__proto__.__proto__ãx.__proto__.__proto__.__proto__ãâ¦â¦ ã¨ãã©ã£ã¦ããã¨ããxã«æ»ã£ã¦ãã¾ããã¨ã¯ãªãã
xã®__proto__ããããã£ã®å¤ï¼ã§ãããªãã¸ã§ã¯ãï¼ããxã®__proto__ãªãã¸ã§ã¯ãã¨å¼ã³ãx, x.__proto__, x.__proto__.__proto__, â¦â¦ ã®ãããªåã__proto__ãã§ã¼ã³ã¨å¼ã³ã¾ãã
__proto__ãã§ã¼ã³ã¯ã__proto__ã®å¤ã«nullãåºç¾ããã¨ããã§çµããã¾ãããµã¤ã¯ã«ãçããªããã¨ã¯ãä¸ã®2çªç®ã®æ§è³ªããä¿è¨¼ããã¾ãã
â__proto__ãã§ã¼ã³ã追ãããã¦ã¿ã
次ã®é¢æ°ã¯ã__proto__ãã§ã¼ã³ãé åã«ãã¦è¿ããã®ã§ãã
function protoChain(x) {
var chain = [];
for (var p = x; p != null; p = p.__proto__) {
chain.push(p);
}
return chain;
}
çµæãè¦ããã表示ããããã«æ¬¡ãå®ç¾©ï¼ã ã ããRhinoç¨ï¼ï¼
/* å¼æ°ã¯é åã ã¨ä»®å® */
function printArray(a) {
for (var i = 0; i < a.length && i < 10; i++) {
print(i + ":" + a[i]);
}
if (i < a.length) {
print("... (more)");
}
}
ãã¼ã¦ã試ãã¦ã¿ã¾ãããã
js> printArray(protoChain(1))
0:1
1:0
2:[object Object]
js> printArray(protoChain("hello"))
0:hello
1:
2:[object Object]
js> printArray(protoChain([1, 2]))
0:1,2
1:
2:[object Object]
js> printArray(protoChain({}))
0:[object Object]
1:[object Object]
js> printArray(protoChain(Number))
0:function Number() { [native code for Number.Number, arity=1] }1:function () {
[native code, arity=0]
}2:[object Object]
js>
æ°å¤1ã®__proto__ãªãã¸ã§ã¯ãã¯0ã§ã0ã®__proto__ãªãã¸ã§ã¯ããåå¨ããããã§ããããã¬ã¼ã³ãªãªãã¸ã§ã¯ã{}ã®__proto__ãã§ã¼ã³ã®é·ãã¯1ï¼ç¾ãããªãã¸ã§ã¯ãæ°ã¯2ï¼ãã®ä»ã¯é·ã2ã«ãªã£ã¦ã¾ããã人çºçã«é·ããã§ã¼ã³ãä½ããã¨ãã§ãã¾ãã
js> var a = {}
js> var b = {}
js> var c = {}
js> b.__proto__ = a
[object Object]
js> c.__proto__ = b
[object Object]
js> printArray(protoChain(c))
0:[object Object]
1:[object Object]
2:[object Object]
3:[object Object]
js>
ãã®ä¾ã§ã¯ããc â b â a â ãªããç¥ããªããªãã¸ã§ã¯ããã¨ãããã§ã¼ã³ã§ãã
ããã§ãæå°æªããã¦ãµã¤ã¯ã«ãä½ã£ã¦ã¿ã¾ãããã
js> var a = {}
js> var b = {}
js> a.__proto__ = b
[object Object]
js> b.__proto__ = a
js: "<stdin>", line 85: Cyclic __proto__ value not allowed.
js>
ãããã«æ絶ããã¾ãããã
â__proto__ãã§ã¼ã³ã使ã£ãããããã£æ¤ç´¢
ãã¦ãä»ã¾ã§èª¿ã¹ã__proto__ãã§ã¼ã³ã¯ä½ã«ä½¿ãããã®ã§ããããããã®çãã¯ããããããã£æ¤ç´¢ã«ä½¿ããããã§ããJavaScriptå¦çç³»ãx.foo
ã¨ããå¼ãè¦ãã¨ãã次ã®ããã«åä½ãã¾ãã
- ãªãã¸ã§ã¯ãxã«"foo"ã¨ããååã®ããããã£ãããã°ããã®å¤ãè¿ãã
- ãªãã¸ã§ã¯ãxã§ããããã£ãè¦ã¤ãããªãã¨ããxã®__proto__ãªãã¸ã§ã¯ãã®"foo"ã¨ããååã®ããããã£ãæ¢ããããã°ããã®å¤ãè¿ãã
- 以ä¸åæ§ã«ã__proto__ãã§ã¼ã³ããã©ã£ã¦ããããã£ãæ¢ãããã§ã¼ã³ããã©ãåã£ã¦ããªãè¦ã¤ãããªããªããundefinedå¤ãè¿ãã
å®é¨ãã¦ã¿ã¾ãã
js> var point = {x:20, y:30}
js> point.y
30
js> point.color
js> typeof point.color
undefined
js> var colored = {color:"black"}
js> point.__proto__ = colored
[object Object]
js> point.color
black
js>
colorã¨ããããããã£ã¯ãªãã¸ã§ã¯ãpointã«å®ç¾©ããã¦ããªãã£ãã®ã§ããããªãã¸ã§ã¯ãcoloredã__proto__ããããã£ã«è¨å®ãããã¨ã«ãããããããpointã«ãcolorããããã£ãåå¨ããããã«æ¯ãèãã¾ãã
JavaScriptã§ã¯ãã¡ã½ããã¯ããããã£ã®ä¸ç¨®ï¼å¤ãé¢æ°ãªãã¸ã§ã¯ãã§ããããããã£ï¼ã§ããããã¡ã½ããæ¤ç´¢ããã®ã¡ã«ããºã ã使ããã¾ãã
âãã®ç¶ãã¨ä»åã®ã¾ã¨ã
ããã§ã__proto__ãªãã¸ã§ã¯ãã®è©±ã¯çµããã§ããprototypeãªãã¸ã§ã¯ãã®è©±ã¯ãã¦ã¾ãããprototypeãªãã¸ã§ã¯ãã¯ãJavaScriptã®ãªãã¸ã§ã¯ãçæã«é¢ããã®ã§æ¬¡åã§æ±ããã¨ã«ãã¾ãã
- JavaScriptã«ã¯ãå®è¡æã®ããããã£æ¤ç´¢ã®ã¡ã«ããºã ãããã
- ã©ããªãªãã¸ã§ã¯ãã«ãã__proto__ã¨ããåã®ããããã£ãããã
- __proto__ããããã£ã®å¤ï¼ã®ãªãã¸ã§ã¯ãï¼ããã©ã£ã¦ããã¨ãã§ã¼ã³ãã§ãããããã__proto__ãã§ã¼ã³ã¨å¼ã¶ã
- ãªãã¸ã§ã¯ãã®ããããã£å¤ãæ±ããã¨ãã__proto__ãã§ã¼ã³ããã©ã£ã¦ããããã£ãæ¢ãã
- ã¡ã½ãããããããã£ã®ä¸ç¨®ãªã®ã§ã__proto__ãã§ã¼ã³ããã©ã£ã¦æ¤ç´¢ãããã