- ã¯ããã«
- ãªããžã§ã¯ãããããã£ã®åå解決
- å€ã®å²ãåœãŠ
- å€ã®èªã¿åã
- èå¥åã®åå解決ãå®è¡ã³ã³ããã¹ããããã³ã¹ã³ãŒããã§ãŒã³
- å®è¡ã³ã³ããã¹ã
- ã¹ã³ãŒããã§ãŒã³ãš[[scope]]ããããã£
- èå¥åã®åå解決
- ...
ã¯ããã«
ç絡
ã¯ããŒãžã£ãŒã¯ãèªç±å€æ°ãæã€ããšãã§ããåŒïŒéåžžã¯é¢æ°ïŒã§ããããããã®å€æ°ããã€ã³ãããç°å¢ïŒã€ãŸãããã®åŒãéããïŒãæã€ããšãã§ããŸãã
ã¯ããŒãžã£ãŒã¯ECMAScriptïŒjavascriptïŒã®æã匷åãªæ©èœã®1ã€ã§ãããç解ãªãããŠé©åã«é©çšããããšã¯ã§ããŸããã å¶ç¶ã§ãç°¡åã«äœæã§ãããšããäºå®ã«ãããããããç¹ã«æ¯èŒçäžè¬çãªãã©ãŠã¶ç°å¢ã§ã¯ãäœæãæ害ãªçµæãããããå¯èœæ§ããããŸãã é害ãšã®å¶çºçãªè¡çªãé¿ããé害ãå©çšããã«ã¯ããã®ã¡ã«ããºã ãç解ããå¿ èŠããããŸãã ããã¯ãèå¥åã®è§£æ±ºã«ãããã¹ã³ãŒããã§ãŒã³ã®åœ¹å²ãšããªããžã§ã¯ãã®ããããã£åã®è§£æ±ºã«å€§ããäŸåããŸãã
ã¯ããŒãžã£ãŒã®æãç°¡åãªèª¬æã¯ãECMAScriptãä»ã®é¢æ°ã®æ¬äœå ã«ãã¹ããããé¢æ°ãé¢æ°å®çŸ©ãããã³é¢æ°åŒãèš±å¯ãããšããããšã§ãã ãŸãããããã®ãã¹ããããé¢æ°ã¯ãå€éšé¢æ°ïŒå€éšé¢æ°ïŒå ã®ãã¹ãŠã®ããŒã«ã«å€æ°ããã©ã¡ãŒã¿ãŒãããã³é¢æ°ã«ã¢ã¯ã»ã¹ã§ããŸãã ãããã®ãã¹ããããé¢æ°ã®1ã€ãå«ãŸããŠããé¢æ°ã®å€éšã§äœ¿çšå¯èœã«ãªããšãã¯ããŒãžã£ãŒã圢æããããããå€éšé¢æ°ã®å®äºåŸã«å®è¡ã§ããŸãã ãã®æç¹ã§ã圌女ã¯ãŸã ããŒã«ã«å€æ°ããã©ã¡ãŒã¿ãŒãããã³å€éšé¢æ°ã®å éšé¢æ°å®£èšã«ã¢ã¯ã»ã¹ã§ããŸãã ãããã®ããŒã«ã«å€æ°ããã©ã¡ãŒã¿ãŒãããã³é¢æ°å®£èšã¯ïŒæåã¯ïŒå€éšé¢æ°ãå®äºãããšããšåãå€ãæã¡ãå éšé¢æ°ãšå¯Ÿè©±ã§ããŸãã
æ®å¿µãªãããã¯ããŒãžã£ãŒãé©åã«ç解ããã«ã¯ãã¯ããŒãžã£ãŒã®èåŸã«ããã¡ã«ããºã ãç解ããå€ãã®æè¡çãªè©³çŽ°ãç¥ãå¿ èŠããããŸãã 以äžã®èª¬æã®åé ã§ECMA 262ã§å®çŸ©ãããŠããã¢ã«ãŽãªãºã ã®äžéšã«ã€ããŠèª¬æããŠããŸãããã»ãšãã©ã®ã¢ã«ãŽãªãºã ãçç¥ããããåçŽåããŠåçŽåãããããããšã¯ã§ããŸããã ãªããžã§ã¯ãã®ããããã£åã®è§£æ±ºã«ç²ŸéããŠããå Žåã¯ããã®ã»ã¯ã·ã§ã³ãã¹ãããã§ããŸãããã¯ããŒãžã£ãŒã«æ¢ã«ç²ŸéããŠãã人ã ããåŸç¶ã®ã»ã¯ã·ã§ã³ãã¹ãããããä»ããèªãã®ããããŠãããã®äœ¿çšã«æ»ãããšãã§ããŸãã
ãªããžã§ã¯ãããããã£ã®åå解決
ECMAScriptã¯ããã€ãã£ããªããžã§ã¯ããšãã¹ããªããžã§ã¯ããããã³çµã¿èŸŒã¿ãªããžã§ã¯ããšåŒã°ãããã€ãã£ããªããžã§ã¯ãã®ãµãã«ããŽãªã®2ã€ã®ã«ããŽãªã®ãªããžã§ã¯ããèªèããŸãïŒECMA 262 3rd Ed Section 4.3ïŒã ãã€ãã£ããªããžã§ã¯ãã¯èšèªã«å±ãããã¹ããªããžã§ã¯ãã¯ç°å¢ã«ãã£ãŠæäŸãããããšãã°ããã¥ã¡ã³ããªããžã§ã¯ããDOMããŒããªã©ã«ãªããŸãã
ãã€ãã£ããªããžã§ã¯ãã¯ãååä»ãããããã£ã®ç¡æã®åçã³ã³ããã§ãïŒäžéšã®å®è£ ã¯ãåã蟌ã¿ãªããžã§ã¯ãã®ãµãã«ããŽãªã«ãªããšããã»ã©åçã§ã¯ãããŸããããéåžžã¯éèŠã§ã¯ãããŸããïŒã ãã€ãã£ããªããžã§ã¯ãã®ç¹å®ã®ååä»ãããããã£ã¯ãå¥ã®ãªããžã§ã¯ãïŒãã®æå³ã§ã¯é¢æ°ã§ããããŸãïŒãžã®åç §ãšãªãå€ããŸãã¯åºæ¬å€ãStringãNumberãBooleanãNullããŸãã¯Undefinedãæ ŒçŽããŸãã ããªããã£ãåUndefinedã¯ããªããžã§ã¯ãã®ããããã£ãundefinedã«èšå®ã§ãããšããæå³ã§å°ãçããã§ãããããããã£ã¯ãªããžã§ã¯ãããåé€ãããŸããã ãªããžã§ã¯ãã®ååä»ãããããã£ã®ãŸãŸã§ãããå€undefinedãåã«æ ŒçŽããŸãã
以äžã¯ããªããžã§ã¯ãã®ããããã£ãã©ã®ããã«èªã¿åãããŠèšå®ãããå éšã®è©³çŽ°ã«å¯èœãªéã圱é¿ãäžãããã«ã€ããŠã®ç°¡åãªèª¬æã§ãã
å€ã®å²ãåœãŠ
ãªããžã§ã¯ãã®ååä»ãããããã£ãäœæããããæ¢åã®ååä»ãããããã£ã®å€ãèšå®ããã«ã¯ãååä»ãããããã£ã«å€ãå²ãåœãŠãŸãã
ãã®ããã«
var objectRef = new Object(); // (generic) javascript.
ãtestNumberããšããååã®ããããã£ã¯ã次ã®ããã«äœæã§ããŸãã
objectRef.testNumber = 5; /* */ objectRef["testNumber"] = 5;
ãªããžã§ã¯ãã«ã¯ãå€ãå²ãåœãŠãåã«testNumberããããã£ããããŸããã§ãããã åŸã§äœæãããŸããã 以éã®å²ãåœãŠã§ã¯ããã®ããããã£ãäœæããå¿ èŠã¯ãããŸãããå€ãå€æŽããã ãã§ãã
objectRef.testNumber = 8; /* */ objectRef["testNumber"] = 8;
Javascriptãªããžã§ã¯ãã«ã¯ã以äžã§ç°¡åã«èª¬æããããã«ãããèªäœããªããžã§ã¯ãã«ãªãå¯èœæ§ã®ãããããã¿ã€ããããããããã®ãããã¿ã€ãã«ã¯ååä»ãããããã£ãå«ããããšãã§ããŸãã ãã ããããã¯å²ãåœãŠã«ã¯é©çšãããŸããã å€ãå²ãåœãŠããããªããžã§ã¯ãã«å¯Ÿå¿ããååã®ããããã£ããªãå Žåããã®ããããã£ãäœæãããå€ãå²ãåœãŠãããŸãã ãªããžã§ã¯ãã«ãã®ãããªããããã£ãããå Žåããã®å€ã¯ãªã»ãããããŸãã
å€ã®èªã¿åã
ãªããžã§ã¯ãã®ãããã¿ã€ãã䜿çšãããã®ã¯ãå€ãèªã¿åããšãã§ãã ãªããžã§ã¯ãã«ããããã£ã¢ã¯ã»ãµãŒåŒã§äœ¿çšãããååã®ããããã£ãããå Žåããã®ããããã£ã®å€ãè¿ãããŸã
/* . , */ objectRef.testNumber = 8; /* */ var val = objectRef.testNumber; /* val 8, . */
ãããããã¹ãŠã®ãªããžã§ã¯ãã¯ãããã¿ã€ããæã€ããšãã§ãããããã¿ã€ãã¯ãªããžã§ã¯ãã§ããããããã¿ã€ããæã€ããšãã§ãããããã¿ã€ããªã©ãæã€ããšãã§ãããããã¿ã€ããã§ãŒã³ãšåŒã°ãããã®ã圢æããŸãã ãããã¿ã€ããã§ãŒã³ã¯ããã§ãŒã³å ã®ãªããžã§ã¯ãã®1ã€ã«nullãããã¿ã€ãããããšçµäºããŸã ã Objectã³ã³ã¹ãã©ã¯ã¿ãŒã§ããã©ã«ãã§äœ¿çšããããããã¿ã€ãã«ã¯ã nullãããã¿ã€ããããããã
var objectRef = new Object(); // (generic) javascript
ãããã¿ã€ãObject.prototypeã䜿çšããŠãªããžã§ã¯ããäœæããŸããããèªäœã¯nullãããã¿ã€ããæã¡ãŸã ã 次ã«ã objectRefãªããžã§ã¯ãã®ãããã¿ã€ããã§ãŒã³ã«ã¯ã Object.prototypeãšãã 1ã€ã®ãªããžã§ã¯ãã®ã¿ãå«ãŸããŸãã ããã
/* ââ MyObject1.*/ function MyObject1(formalParameter){ /* testNumber , , */ this.testNumber = formalParameter; } /* ââ MyObject2 */ function MyObject2(formalParameter){ /* testString , , */ this.testString = formalParameter; } /* , , MyObject2, MyObject1, 8 MyObject1, testNumber */ MyObject2.prototype = new MyObject1( 8 ); /*, MyObject2 objectRef , */ var objectRef = new MyObject2( "String_Value" );
å€æ°objectRefã«ãã£ãŠåç §ãããMyObject2ã®ã€ã³ã¹ã¿ã³ã¹ã«ã¯ããããã¿ã€ããã§ãŒã³ããããŸãã ãã®ãã§ãŒã³ã®æåã®ãªããžã§ã¯ãã¯MyObject1ã®ã€ã³ã¹ã¿ã³ã¹ã§ãäœæããã MyObject2ã³ã³ã¹ãã©ã¯ã¿ãŒã®prototypeããããã£ã«å²ãåœãŠãããŠããŸã ã MyObject1ã®ã€ã³ã¹ã¿ã³ã¹ã«ã¯ãããã©ã«ãã§MyObject1ã®prototypeããããã£ã«å²ãåœãŠããããããã¿ã€ããªããžã§ã¯ãããããŸãã ãã®ãããã¿ã€ãã¯Objectã®ããã©ã«ãã®ãããã¿ã€ãã§ãã Object.prototypeã«ãã£ãŠåç §ããããªããžã§ã¯ãã Object.prototypeã«ã¯nullãããã¿ã€ããããããããã§ãŒã³ã¯ãã®æç¹ã§çµäºããŸãã
ããããã£ã¢ã¯ã»ã¹åŒãobjectRefã«ãã£ãŠåç §ããããªããžã§ã¯ãããååä»ãããããã£ãèªã¿åãããšãããšããããã¿ã€ããã§ãŒã³å šäœãããã»ã¹ã«åå ã§ããŸãã åçŽãªå Žå
var val = objectRef.testString;
-objectRefãä»ããŠã¢ã¯ã»ã¹å¯èœãªMyObject2ã®ã€ã³ã¹ã¿ã³ã¹ã«ã¯testStringãšããããããã£ãããããããã®ããããã£ã®å€ã¯String_Valueã«èšå®ãããå€æ°valã«å²ãåœãŠãããŸãã
ããã
var val = objectRef.testNumber;
MyObject2é¢æ°ã®ã€ã³ã¹ã¿ã³ã¹ããããããã£ãèªã¿åãããšãã§ããªãããã ãã®ãããªããããã£ã¯ãããŸããããå€æ°val㯠8ã«èšå®ããã undefinedã§ã¯ãããŸãããããã¯ããªããžã§ã¯ãèªäœã®å¯Ÿå¿ããååä»ãããããã£ã®æ€çŽ¢ã倱æãããããã€ã³ã¿ããªã¿ããã®ãããã¿ã€ãã§ãããªããžã§ã¯ãããã§ãã¯ããããã§ã ãã®ãããã¿ã€ãã¯ããã®ããããã£ã«å€8ãå²ãåœãŠãããtestNumberããããã£ã§äœæãããé¢æ°MyObject1ã®ã€ã³ã¹ã¿ã³ã¹ã§ãããããããããã£ãžã®ã¢ã¯ã»ã¹ã®åŒã¯å€8ãšããŠèšç®ãããŸãã MyObject1ãMyObject2ãtoStringã¡ãœãããå®çŸ©ããŸããããããããã£ã¢ã¯ã»ã¹åŒãobjectRefããtoStringããããã£ã®å€ãèªã¿åãããšãããšã
var val = objectRef.toString;
次ã«ã valå€æ°ã«é¢æ°åç §ãå²ãåœãŠãããŸãã ãã®é¢æ°ã¯Object.prototypeãªããžã§ã¯ãã®toStringããããã£ã§ããããããã¿ã€ãæ€èšŒããã»ã¹ã®çµæãšããŠè¿ãããŸãïŒ objectRefãªããžã§ã¯ãããã§ãã¯ãã ããã®äžã«toStringããããã£ãæ¬ èœããåŸã objectRefãããã¿ã€ãããã§ãã¯ãã ããã®ããããã£ããªãããšãå€æãããšãã®ãããã¿ã€ãã¯ãã¹ãäžã§ãã ãã®ãããã¿ã€ãã¯Object.prototypeã§ã toStringã¡ãœãããããããªã³ã¯ã¯ãã®é¢æ°ã«æ»ããŸãã
çµè«ãšããŠïŒ
var val = objectRef.madeUpProperty;
-ãããã¿ã€ããã§ãŒã³ãåŠçããããã»ã¹ãã©ã®ãªããžã§ã¯ãã«ãmadeUpPropertyãšããååã®ããããã£ãèŠã€ããããªãããã undefinedãè¿ããæçµçã«Object.prototypeãªããžã§ã¯ãã®ãããã¿ã€ãã«å°éããŸãã nullã®å Žåã undefinedãè¿ãããšã§ããã»ã¹ãçµäºããŸãã
ååä»ãããããã£ãèªã¿åããšããªããžã§ã¯ããŸãã¯ãã®ãããã¿ã€ããã§ãŒã³ããæåã«èŠã€ãã£ãå€ãè¿ãããŸãã 察å¿ããããããã£ãååšããªãå Žåããªããžã§ã¯ãã®ååä»ãããããã£ã«å€ãå²ãåœãŠããšããªããžã§ã¯ãèªäœã«ããããã£ãäœæãããŸãã
ããã¯ãå€ãobjectRef.testNumber = 3ã«å²ãåœãŠãããå Žåã testNumberããããã£ãMyObject2é¢æ°ã®ã€ã³ã¹ã¿ã³ã¹ã§äœæããããã®åŸå€ãèªã¿åãããšãããšããªããžã§ã¯ãã«èšå®ãããå€ãè¿ãããããšãæå³ããŸãã ããã§ãããããã£ã®ã¢ã¯ã»ã¹åŒãå®è¡ããããã«ãããã¿ã€ããã§ãŒã³ã¯äžèŠã«ãªããŸãããã testNumberããããã£ã«å€8ãå²ãåœãŠãããMyObject1ã®ã€ã³ã¹ã¿ã³ã¹ã¯å€æŽãããŠããŸããã objectRefãªããžã§ã¯ãã«å€ãå²ãåœãŠããšããã®ãããã¿ã€ããã§ãŒã³å ã®å¯Ÿå¿ããããããã£ãåçŽã«é衚瀺ã«ãªããŸãã
ECMAScript ãå éšåObjectã®å éšããããã£[[prototype]]ãå®çŸ©ããŠããããšã«æ³šæããŠãã ããã ãã®ããããã£ã¯ã¹ã¯ãªããããçŽæ¥ã¢ã¯ã»ã¹ã§ããŸããããã¢ã¯ã»ã¹ããããã£åŒã解決ããããã«äœ¿çšãããå éš[[prototype]]ããããã£ã«ãã£ãŠåç §ããããªããžã§ã¯ãã®ãã§ãŒã³ããããŸãã ãã®ãã§ãŒã³ã¯ããªããžã§ã¯ãã®ãããã¿ã€ããã§ãŒã³ã§ãã ãããªãã¯ãããã¿ã€ãããããã£ã¯ãå éš[[prototype]]ããããã£ãšå ±ã«ãå€ã®å²ãåœãŠããããã¿ã€ãã®å®çŸ©ãããã³æäœã®ããã«ååšããŸãã èèã®ããã2ã€ã®ç¹æ§éã®é¢ä¿ã®è©³çŽ°ã¯ECMA 262ïŒç¬¬3çïŒã§èª¬æãããŠããããã®èª¬æã«ã¯å«ãŸããŠããŸããã
èå¥åã®åå解決ãå®è¡ã³ã³ããã¹ããããã³ã¹ã³ãŒããã§ãŒã³
å®è¡ã³ã³ããã¹ã
å®è¡ã³ã³ããã¹ãã¯ãECMAScriptå®è£ ïŒECMA 262第3çïŒã§äœ¿çšãããæœè±¡æŠå¿µã§ãECMAScriptå®è£ ã«å¿ èŠãªåäœã決å®ããŸãã ä»æ§ã«ã¯ã å®è¡ã³ã³ããã¹ãã®å®è£ æ¹æ³ã«ã€ããŠã¯äœãèšèŒãããŠããŸãããã å®è¡ã³ã³ããã¹ãã«ã¯ãä»æ§ã§å®çŸ©ãããæ§é ãåç §ããé¢é£å±æ§ããããããéããããŠããŠããããããã£ãæã€ãªããžã§ã¯ããšããŠæ§æ³ïŒãŸãã¯å®è£ ïŒããããšãã§ããŸãã
ãã¹ãŠã®javascriptã³ãŒãã¯ãå®è¡ã³ã³ããã¹ãã§å®è¡ãããŸã ã ã°ããŒãã«ã³ãŒãïŒhtmlããŒãžãŸãã¯JSãã¡ã€ã«ã«åã蟌ãŸããã³ãŒãããŸãã¯ããŒãžã®ããŒãïŒããŒãïŒåŸã«å®è¡ãããã³ãŒãïŒã¯ã°ããŒãã«å®è¡ã³ã³ããã¹ãã§å®è¡ãããåé¢æ°åŒã³åºãïŒããããã³ã³ã¹ãã©ã¯ã¿ãŒãšããŠïŒã«ã¯å¯Ÿå¿ããå®è¡ã³ã³ããã¹ãããããŸãã evalé¢æ°ã䜿çšããŠå®è¡ãããã³ãŒããç¹å®ã®å®è¡ã³ã³ããã¹ããååŸããŸããã evalã¯javascriptããã°ã©ããŒã«ãã£ãŠäžè¬çã«äœ¿çšãããªããããããã§ã¯èª¬æããŸããã å®è¡ã³ã³ããã¹ãã®æå®ããã詳现ã¯ãECMA 262ïŒç¬¬3çïŒã®ã»ã¯ã·ã§ã³10.2ã«ãããŸãã
javascripté¢æ°ãåŒã³åºããããšã å®è¡ã³ã³ããã¹ããè¿œå ãããŸã ãå¥ã®é¢æ°ãåŒã³åºããããšïŒãŸãã¯åãé¢æ°ãååž°çã«ïŒãæ°ããå®è¡ã³ã³ããã¹ããäœæãããå®è¡ããã»ã¹ã¯é¢æ°åŒã³åºãäžã«ãã®ã³ã³ããã¹ããè¿œå ããŸãã åŒã³åºãããé¢æ°ãå®äºãããšãå ã®å®è¡ã³ã³ããã¹ããè¿ãããŸãã ãããã£ãŠãå®è¡ããJavaScriptã³ãŒãã¯ã å®è¡ã³ã³ããã¹ãã®ã¹ã¿ãã¯ã圢æããŸãã
å®è¡ã³ã³ããã¹ããäœæããããšãç¹å®ã®é åºã§ããã€ãã®ããšãèµ·ãããŸãã æåã«ã å®è¡ã®ã³ã³ããã¹ã㧠ãActivationãªããžã§ã¯ããäœæãããŸãã ã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ãã¯ãä»æ§ã®å¥ã®ã¡ã«ããºã ã§ãã çµæãšããŠã¢ã¯ã»ã¹å¯èœãªååä»ãããããã£ãããããããªããžã§ã¯ããšèŠãªãããšãã§ããŸãããéåžžã®ãªããžã§ã¯ãã§ã¯ãããŸããã ãããã¿ã€ãããããŸããïŒå°ãªããšããããã¿ã€ãã¯å®çŸ©ãããŠããŸããïŒãjavascriptã³ãŒãã«ã¯ããªã³ã¯ããããŸããã
é¢æ°ãåŒã³åºãããã®å®è¡ã³ã³ããã¹ããäœæãã次ã®ã¹ãããã¯ã åŒæ°ãªããžã§ã¯ããäœæããããšã§ããããã¯ããã®é åºã§é¢æ°ã«éä¿¡ãããåŒæ°ã«å¯Ÿå¿ããæŽæ°ã§ã€ã³ããã¯ã¹ä»ããããèŠçŽ ãæã€é åã®ãããªãªããžã§ã¯ãã§ãã ãŸããããããã£lengthãšcalleeããããŸãïŒããã¯èª¬æã«é¢ä¿ãããŸããã詳现ã¯ä»æ§ã«ãããŸãïŒã ã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ãã®ããããã£ã¯ã åŒæ°argumentsãžã®åç §ãå²ãåœãŠãããargumentsãšããååã§äœæãããŸãã
次ã«ãå®è¡ã³ã³ããã¹ãã scopeãå²ãåœãŠãŸã ã ã¹ã³ãŒãã¯ããªããžã§ã¯ãã®ãªã¹ãïŒãŸãã¯ãã§ãŒã³ïŒã§æ§æãããŸãã åé¢æ°ãªããžã§ã¯ãã«ã¯å éš[[scope]]ããããã£ãããïŒããã«ã€ããŠã¯åŸã§è©³ãã説æããŸãïŒãããããªããžã§ã¯ãã®ãªã¹ãïŒãŸãã¯ãã§ãŒã³ïŒã§æ§æãããŠããŸãã é¢æ°åŒã³åºãã®å®è¡ã³ã³ããã¹ãã«å²ãåœãŠãããã¹ã³ãŒãã¯ã察å¿ããé¢æ°ãªããžã§ã¯ãã®[[scope]]ããããã£ã«ãã£ãŠåç §ããããªã¹ããšããã§ãŒã³ã®æäžéšïŒãŸãã¯ãã®ãªã¹ãã®æäžéšïŒã«è¿œå ãããã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ãã§æ§æãããŸãã
次ã«ãECMA 262ã§å€æ°ã®ãªããžã§ã¯ãïŒå€æ°ãªããžã§ã¯ãïŒãšããŠå®çŸ©ãããŠãããªããžã§ã¯ãã䜿çšããŠãå€æ°ãäœæããããã»ã¹ïŒå€æ°ã®ã€ã³ã¹ã¿ã³ã¹åïŒãçºçããŸãã åæã«ãã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ãã¯å€æ°ã®ãªããžã§ã¯ããšããŠäœ¿çšãããŸãïŒããã¯åããªããžã§ã¯ãã§ãããããã¯éèŠã§ãïŒã å€æ°ãªããžã§ã¯ãã®ååä»ãããããã£ã¯ãé¢æ°ã®åä»®ãã©ã¡ãŒã¿ãŒã«å¯ŸããŠäœæãããé¢æ°ãåŒã³åºããšãã®åŒæ°ããããã®ãã©ã¡ãŒã¿ãŒã«å¯Ÿå¿ããå ŽåãåŒæ°ã¯ãããã®ããããã£ã«å²ãåœãŠãããŸãïŒããã§ãªãå Žåã¯undefined ïŒã å éšé¢æ°ã®å®çŸ©ã¯ãé¢æ°å®£èšã§äœ¿çšãããé¢æ°ã®ååã«å¯Ÿå¿ããååãæã€å€æ°ã®ãªããžã§ã¯ãããããã£ã«å²ãåœãŠãããé¢æ°ãªããžã§ã¯ããäœæããããã«äœ¿çšãããŸãã å€æ°ãäœæããæåŸã®æé ã¯ãé¢æ°å ã§å®çŸ©ããããã¹ãŠã®ããŒã«ã«å€æ°ã«å¯Ÿå¿ããå€æ°ãªããžã§ã¯ãã®ååä»ãããããã£ãäœæããããšã§ãã
宣èšãããããŒã«ã«å€æ°ã«å¯Ÿå¿ããå€æ°ãªããžã§ã¯ãã§äœæãããããããã£ã¯ãå€æ°ã®äœæäžã«æåã«æªå®çŸ©ã®å€ãååŸããŸããããŒã«ã«å€æ°ã¯ãé¢æ°æ¬äœã³ãŒãã®å®è¡äžã«å¯Ÿå¿ããå²ãåœãŠæäœãå®è¡ããããŸã§åæåãããŸããã
å®éãé¢æ°ã®ããŒã«ã«å€æ°ã«å¯Ÿå¿ããargumentsããããã£ãæã€ã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ããšååä»ãããããã£ãæã€å€æ°ãªããžã§ã¯ãã¯ãèå¥åã®åŒæ°ãé¢æ°ã®ããŒã«ã«å€æ°ãšèŠãªãããšãã§ããåããªããžã§ã¯ãã§ãã
çµè«ãšããŠãthisããŒã¯ãŒãã§äœ¿çšãããå€ãå²ãåœãŠãããŸãã ãã®å€ããªããžã§ã¯ããåç §ããå Žåã ãã®æ¥é èŸãä»ããããããã£ã¢ã¯ã»ã¹åŒã¯ãã®ãªããžã§ã¯ãã®ããããã£ãåç §ããŸãã ãã®å€ïŒé¢æ°å ã§å²ãåœãŠãããïŒãnullã®å Žåã ããã¯ã°ããŒãã«ãªããžã§ã¯ããåç §ããŸãã
ã°ããŒãã«å®è¡ã³ã³ããã¹ãã®åŠçã¯ãããã«ç°ãªããåŒæ°ããªããããåç §ããã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ããå®çŸ©ããå¿ èŠã¯ãããŸããã ã°ããŒãã«å®è¡ã³ã³ããã¹ãã«ã¯ã¹ã³ãŒããå¿ èŠã§ããããã®ãã§ãŒã³ã«ã¯ã°ããŒãã«ãªããžã§ã¯ããšãã1ã€ã®ãªããžã§ã¯ãã®ã¿ãå«ãŸããŸãã ã°ããŒãã«å®è¡ã³ã³ããã¹ãã¯å€æ°ã®äœæãééãããã®å éšé¢æ°ã¯éåžžã®ãããã¬ãã«ã®å®£èšé¢æ°ã§ãããã»ãšãã©ã®JavaScriptã³ãŒããæ§æããŸãã ã°ããŒãã«ãªããžã§ã¯ãã¯å€æ°ã®ãªããžã§ã¯ããšããŠäœ¿çšããããããã°ããŒãã«ã«å®£èšãããé¢æ°ã¯ã°ããŒãã«ãªããžã§ã¯ãã®ããããã£ã«ãªããŸãã ã°ããŒãã«ã«å®£èšãããå€æ°ã«ã€ããŠãåãã§ãã
ã°ããŒãã«å®è¡ã³ã³ããã¹ãã¯ã thisã®å€ãšããŠã°ããŒãã«ãªããžã§ã¯ããžã®åç §ã䜿çšããŸãã
ã¹ã³ãŒããã§ãŒã³ãš[[scope]]ããããã£
é¢æ°åŒã³åºãã®å®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒããã§ãŒã³ã¯ãé¢æ°ãªããžã§ã¯ãã®[[scope]]ããããã£ã«å«ãŸããã¹ã³ãŒããã§ãŒã³ã®å é ã«ã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ãïŒå€æ°ãªããžã§ã¯ãïŒãè¿œå ããããšã«ãã£ãŠçµã¿ç«ãŠãããŸãããããã£ãŠãå éš[[scope]]ããããã£ã®å®çŸ©æ¹æ³ãç解ããããšãéèŠã§ãã
ECMAScriptã§ã¯ãé¢æ°ã¯ãªããžã§ã¯ãã§ãããé¢æ°å®£èšããå€æ°ãäœæãããšããåŒé¢æ°ïŒé¢æ°åŒïŒãå®è¡ãããšãããŸãã¯Functionã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºããããšãã«äœæãããŸãã
Functionã³ã³ã¹ãã©ã¯ã¿ãŒã«ãã£ãŠäœæãããé¢æ°ãªããžã§ã¯ãã«ã¯ãåžžã«[[scope]]ããããã£ããããŸããããã¯ãã°ããŒãã«ãªããžã§ã¯ãã®ã¿ãå«ãã¹ã³ãŒããã§ãŒã³ãåç §ããŸãã
é¢æ°å®£èšãŸãã¯é¢æ°åŒã«ãã£ãŠäœæãããé¢æ°ãªããžã§ã¯ãã«ã¯ãããããäœæãããå®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒãããããå éš[[scope]]ããããã£ã«å²ãåœãŠãããŸãã
次ã®ãããªã°ããŒãã«é¢æ°å®£èšã®æãåçŽãªå Žå
function exampleFunction(formalParameter){ ... // }
察å¿ããé¢æ°ãªããžã§ã¯ãã¯ãã°ããŒãã«å®è¡ã³ã³ããã¹ãã®å€æ°ã®äœæäžã«äœæãããŸãã ã°ããŒãã«å®è¡ã³ã³ããã¹ãã«ã¯ãã°ããŒãã«ãªããžã§ã¯ãã®ã¿ã§æ§æãããã¹ã³ãŒãããããŸãã
ãããã£ãŠãã°ããŒãã«ãªããžã§ã¯ãexampleFunctionã®ããããã£ã«ãã£ãŠåç §ããããã®äœæãããé¢æ°ãªããžã§ã¯ãã«ã¯ãã°ããŒãã«ãªããžã§ã¯ãã®ã¿ãå«ãã¹ã³ãŒããã§ãŒã³ãåç §ããå éšããããã£[[scope]]ãå²ãåœãŠãããŸãã
ã°ããŒãã«ã³ã³ããã¹ãã§ã®åŒé¢æ°ã®å®è¡äžã«ãåæ§ã®ã¹ã³ãŒããã§ãŒã³ãå²ãåœãŠãããŸãã
var exampleFuncRef = function(){ ... // }
ãã ãããã®å Žåãã°ããŒãã«ãªããžã§ã¯ãã®ååä»ãããããã£ã¯ãã°ããŒãã«å®è¡ã³ã³ããã¹ãã®å€æ°ã®äœæäžã«äœæãããŸãããé¢æ°ãªããžã§ã¯ãã¯äœæããããå²ãåœãŠåŒãå®è¡ããããŸã§ãã°ããŒãã«ãªããžã§ã¯ãã®ååä»ãããããã£ã«ãã®åç §ãå²ãåœãŠãããŸããã ãã ããé¢æ°ã®äœæã¯åŒãç¶ãã°ããŒãã«å®è¡ã³ã³ããã¹ãã§è¡ããããããäœæãããé¢æ°ãªããžã§ã¯ãã®[[scope]]ããããã£ã«ã¯ãå²ãåœãŠãããã¹ã³ãŒããã§ãŒã³ã®ã°ããŒãã«ãªããžã§ã¯ãã®ã¿ãå«ãŸããŸãã
é¢æ°ãšåŒé¢æ°ã®å éšå®£èšã«ãããé¢æ°å®è¡ã®ã³ã³ããã¹ãå ã§äœæãããé¢æ°ãªããžã§ã¯ããäœæããããããã¹ã³ãŒãã®ããè€éãªãã§ãŒã³ãååŸãããŸã ã å éšé¢æ°ã®å®£èšã§é¢æ°ãå®çŸ©ããå€éšé¢æ°ãå®è¡ãã以äžã®ã³ãŒããæ€èšããŠãã ãã
function exampleOuterFunction(formalParameter){ function exampleInnerFuncitonDec(){ ... // } ... // } exampleOuterFunction( 5 );
å€éšé¢æ°ã®å®£èšã«å¯Ÿå¿ããé¢æ°ãªããžã§ã¯ãã¯ãã°ããŒãã«å®è¡ã³ã³ããã¹ãã§ã®å€æ°ã®äœæäžã«äœæãããããããã®[[scope]]ããããã£ã«ã¯ã1ã€ã®ã°ããŒãã«ãªããžã§ã¯ãã§æ§æãããã¹ã³ãŒã ãã§ãŒã³ãå«ãŸããŸãã
ã°ããŒãã«ã³ãŒããexampleOuterFunctionåŒã³åºããè¡ããšãã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ãïŒå€æ°ãªããžã§ã¯ãïŒãšãšãã«ããã®é¢æ°åŒã³åºãã®æ°ããå®è¡ã³ã³ããã¹ããäœæãããŸãã æ°ããå®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒãã¯ãæ°ããã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ããšãããã«ç¶ãå€éšé¢æ°ã®[[scope]]ããããã£ãåç §ãããã§ãŒã³ïŒã°ããŒãã«ãªããžã§ã¯ãã®ã¿ïŒã§æ§æããããã§ãŒã³ã«ãªããŸãã æ°ããå®è¡ã³ã³ããã¹ãã®å€æ°ãäœæããããã»ã¹ã«ãããå éšé¢æ°ã®å®£èšã«å¯Ÿå¿ããé¢æ°ãªããžã§ã¯ããäœæãããŸãããã®å éšé¢æ°ã®[[scope]]ããããã£ã«ã¯ãäœæãããå®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒãã®å€ãå²ãåœãŠãããŸãã ã¹ã³ãŒããã§ãŒã³ã«ã¯ãã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ããšããã«ç¶ãã°ããŒãã«ãªããžã§ã¯ããå«ãŸããŸãã
ãã®æç¹ãŸã§ããã¹ãŠã¯ãœãŒã¹ã³ãŒãã®æ§é ãšå®è¡ã«ãã£ãŠèªåçã«å®è¡ããã³å¶åŸ¡ãããŸãã å®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒãã®ãã§ãŒã³ã¯ ãäœæãããé¢æ°ãªããžã§ã¯ãã®[[scope]]ããããã£ãå®çŸ©ãã [[scope]]é¢æ°ãªããžã§ã¯ãã®ãããã®ããããã£ã¯ïŒå¯Ÿå¿ããã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ããšå ±ã«ïŒå®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒãã決å®ããŸãã ãã ããECMAScriptã¯ãã¹ã³ãŒããå€æŽããæ段ãšããŠwithã¹ããŒãã¡ã³ãããµããŒãããŠããŸãã
withã¹ããŒãã¡ã³ãã¯åŒãè©äŸ¡ããããããªããžã§ã¯ãã§ããå ŽåãçŸåšã®å®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒãã«è¿œå ãããŸãïŒã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ã/å€æ°ã®çŽåïŒã 次ã«ãwithã¯å¥ã®åŒïŒãããã¯ã®å ŽåãããïŒ ãè©äŸ¡ããå®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒããã§ãŒã³ãå ã®åœ¢åŒã«åŸ©å ããŸã ã
é¢æ°ãªããžã§ã¯ãã®äœæã¯å€æ°ã®äœæäžã«è¡ãããããã withã¹ããŒãã¡ã³ãã¯é¢æ°ã®å®£èšã«åœ±é¿ããŸããããåŒé¢æ°ã¯withã¹ããŒãã¡ã³ãå ã§å®è¡ã§ããŸã
/* y, */ var y = {x:5}; // x function exampleFuncWith(){ var z; /* , y, */ with(y){ /* -, , z */ z = function(){ ... // -; } } ... } /* exampleFuncWith */ exampleFuncWith();
exampleFuncWithãåŒã³åºããããš ããã®å®è¡ã³ã³ããã¹ãã«ã¯ããã®é¢æ°ã®ã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ããšæ¬¡ã®ã°ããŒãã«ãªããžã§ã¯ãã§æ§æãããã¹ã³ãŒããã§ãŒã³ããããŸãã withã¹ããŒãã¡ã³ããå®è¡ãããš ããã®åŒé¢æ°ã®å®è¡äžãã°ããŒãã«å€æ°yã«ãã£ãŠåç §ããããªããžã§ã¯ãããã®ã¹ã³ãŒããã§ãŒã³ã®å é ã«è¿œå ãããŸããåŒé¢æ°ã®å®è¡ã«ãã£ãŠäœæããã[[scope]]é¢æ°ãªããžã§ã¯ãã®ããããã£ã«ã¯ããã®ãªããžã§ã¯ããäœæãããå®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒãã«å¯Ÿå¿ããå€ãå²ãåœãŠãããŸããã¹ã³ãŒãã¯ããªããžã§ã¯ãyãšãããã«ç¶ãå€éšé¢æ°ã®å®è¡ã®ã³ã³ããã¹ãããã®ã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ããã°ããŒãã«ãªããžã§ã¯ãããæ§æãããŸããwith
ã¹ããŒãã¡ã³ãã®blockã¹ããŒãã¡ã³ããå®äºãããšãå®è¡ã³ã³ããã¹ãã®ã¹ã³ãŒãã埩å ãããŸãïŒyãªããžã§ã¯ãã¯åé€ãããŸãïŒãããã®æç¹ã§é¢æ°ãªããžã§ã¯ããäœæããããã®[[scope]]ããããã£ã«ã¹ã³ãŒããã§ãŒã³ãžã®ãªã³ã¯ãå²ãåœãŠãããŸãããªããžã§ã¯ãyãäžéšã«ãããŸãã
èå¥åã®åå解決
èå¥åã¯ãã¹ã³ãŒãã®ãã§ãŒã³ãéããŠæ€çŽ¢ãããŸããECMA 262ã¯ããããèå¥åã§ã¯ãªãããŒã¯ãŒããšããŠå®çŸ©ããŸãããããã¯äžåçã§ã¯ãããŸãããã¹ã³ãŒãã®ãã§ãŒã³ã«é Œãããšãªããããã䜿çšãããå®è¡ã®ã³ã³ããã¹ãã«ããthisã®å€ã«å¿ããŠæ±ºå®ãããŸãã
èå¥åå解決ã¯ãã¹ã³ãŒããã§ãŒã³ã®æåã®ãªããžã§ã¯ãããå§ãŸããŸããããã»ã¹ã¯ãèå¥åã«å¯Ÿå¿ããååã®ããããã£ããããã©ããã確èªããŸããäºå®ã®ããã®ã¹ã³ãŒãã®é£éãªããžã§ã¯ãã®ãã§ãŒã³ã§ããå Žåããã®ãã¹ãã¯ãã®ãªããžã§ã¯ãã®ãããã¿ã€ãã®ãã§ãŒã³ïŒååšããå ŽåïŒãã«ããŒããŸãã察å¿ããå€ãã¹ã³ãŒããã§ãŒã³ã®æåã®ãªããžã§ã¯ãã§èŠã€ãããªãå Žåãæ€çŽ¢ã¯æ¬¡ã®ãªããžã§ã¯ãã§ç¶è¡ãããŸãããããŠãèå¥åã«å¯Ÿå¿ããååãæã€ããããã£ããã§ãŒã³ã®ãªããžã§ã¯ãã®1ã€ïŒãŸãã¯ãã®ãããã¿ã€ãã®1ã€ïŒã§èŠã€ãããŸã§ããŸãã¯å¯èŠé åã®ãã§ãŒã³ãçµäºãããŸã§ç¶ããŸãã
èå¥åã«å¯Ÿããæäœã¯ãåè¿°ã®ããã«ããªããžã§ã¯ãã®ããããã£ã«ã¢ã¯ã»ã¹ããããã®åŒã䜿çšãããã®ãšåãæ¹æ³ã§çºçããŸããã¹ã³ãŒããã§ãŒã³ã§èå¥ããããªããžã§ã¯ã察å¿ããããããã£ãæã€ããã«ãããããã£ãžã®ã¢ã¯ã»ã¹ã®åŒã§ãªããžã§ã¯ãã®å Žæãåããèå¥åã¯ãã®ãªããžã§ã¯ãã®ããããã£ã®ååãšããŠæ©èœããŸããã°ããŒãã«ãªããžã§ã¯ãã¯åžžã«ã¹ã³ãŒããã§ãŒã³ã®æåŸã«ãããŸãã
é¢æ°åŒã³åºãã®å®è¡ã³ã³ããã¹ãã«ã¯ãã§ãŒã³ã®å é ã«ã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ãïŒå€æ°ãªããžã§ã¯ãïŒãå«ãŸããŠãããããé¢æ°æ¬äœã§äœ¿çšãããèå¥åã¯ãæåã«ããããæ£åŒãªãã©ã¡ãŒã¿ãŒãå éšé¢æ°å®£èšã®ååããŸãã¯ããŒã«ã«å€æ°ã«å¯Ÿå¿ãããã©ããã確èªããŸãããããã¯ãã¢ã¯ãã£ããŒã·ã§ã³ãªããžã§ã¯ãïŒå€æ°ãªããžã§ã¯ãïŒã®ååä»ãããããã£ãšããŠèšç®ãããŸãã
翻蚳ã«ã¯ãªã¥ããã©ã»ãªãããããå«ãŸããŠããŸããã
JavaScriptã®ã¯ããŒãžã£ãŒ[ããŒã2]