Hexletã§ã¯ãããã³ããšã³ãã§ã®æçœãªã¿ã¹ã¯ã ãã§ãªããããšãã°ãReactã§ãã©ãŠã¶ãŒéçºç°å¢ïŒãªãŒãã³ãœãŒã¹ã®hexlet-ide ïŒãå®è£ ããããã«ãJavaScriptã䜿çšããŠããŸãã JavaScriptã®å®çšçãªã³ãŒã¹ããããã¯ããŒãžã£ã«é¢ããã¬ãã¹ã³ã®1ã€ã§ãã ããã¯ãäžè¬çãªããã°ã©ãã³ã°ã®ããã«ãJSå ã§ã¯ãªãéèŠãªãããã¯ã§ãã ä»ã®ã³ãŒã¹ã§èª¬æããŸãã
äžè¬ã«ãJSã§ã¯ããŒãžã£ãŒã䜿çšããããšã«é¢ããå€ãã®èšäºãšãã¥ãŒããªã¢ã«ããããŸãããããããå éšã§ã©ã®ããã«æ©èœãããã«ã€ããŠã®èª¬æã¯ã»ãšãã©ãããŸããã ä»æ¥ã®ç¿»èš³ã¯ããã®ãããã¯å°çšã§ãã JSã®ã¯ããŒãžã£ãŒãäœæããã³ç Žæ£ããããšãã«æ©èœããçç±ãšçç±ãJSã®ãã¹ãŠã®é¢æ°ãã¯ããŒãžã£ãŒã§ããçç±ã
ç§ã¯ããªãé·ãéã¯ããŒãžã£ãŒã䜿çšããŠããŸãã ãããã®äœ¿ãæ¹ãåŠã³ãŸããããå®éã«ã©ã®ããã«æ©èœããã®ãããããŒãã®äžã§ãäœãèµ·ããã®ããå®å šã«ã¯ç解ããŠããŸããã§ããã ããã¯äœã§ããïŒ ãŠã£ãããã£ã¢ã¯æ¬åœã«åœ¹ã«ç«ã¡ãŸããã ã¯ããŒãžã£ãŒã¯ãã€äœæããã³ç Žæ£ãããŸããïŒ å®è£ ã¯ã©ã®ããã«èŠããŸããïŒ
"use strict"; var myClosure = (function outerFunction() { var hidden = 1; return { inc: function innerFunction() { return hidden++; } }; }()); myClosure.inc(); // 1 myClosure.inc(); // 2 myClosure.inc(); // 3 // , . ? // ?
æçµçã«ãã¹ãŠãç解ãããšããç§ã¯ãããã¿ããªãšå ±æãããã£ãã å°ãªããšããç§èªèº«ã¯å¿ããªãã§ãããã ãã£ã±ã
æããŠãã ãã-ãããŠãç§ã¯å¿ããŠãèŠããŠãã ãã-ãããŠãç§ã¯ãããèŠããŠãããããããŠãã ãã-ãããŠãç§ã¯ç解ããŸãã
-ååãšãã³ãžã£ãã³ã»ãã©ã³ã¯ãªã³
å匷ã®éçšã§ãç§ã¯ãšã³ãã£ãã£ã®çžäºäœçšãèŠèŠåããããšããŸããïŒãªããžã§ã¯ãããäºããã©ã®ããã«åç §ããããäžæ¹ãä»æ¹ããç¶æ¿ãããæ¹æ³ãªã©ã ã€ã©ã¹ããèŠã€ãããªãã£ãã®ã§ãèªåã§æããŸããã
èªè ã¯JavaScriptã«ç²ŸéããŠããããšãã°ããŒãã«ãªããžã§ã¯ãã«ã€ããŠç¥ã£ãŠããããšãJSã®é¢æ°ãé«éé¢æ°ã§ããããšãªã©ãç¥ã£ãŠãããšæããŸãã
ã¹ã³ãŒããã§ãŒã³
JSã³ãŒãã®å®è¡äžã¯ãããŒã«ã«å€æ°ãä¿åããããã®ã¹ããŒã¹ãå¿ èŠã§ãã ãã®ã¹ããŒã¹ãã¹ã³ãŒããªããžã§ã¯ãïŒå¥åLexicalEnvironment-åå¥ç°å¢ïŒãŸãã¯åã«ã¹ã³ãŒããªããžã§ã¯ããšåŒã³ãŸãããã ããšãã°ãé¢æ°ãåŒã³åºããŠããŒã«ã«å€æ°ãèšå®ãããšããã®å€æ°ã¯ã¹ã³ãŒããªããžã§ã¯ãã«æ ŒçŽãããŸãã ãããéåžžã®JavaScriptãªããžã§ã¯ããšèããããšãã§ããŸããã1ã€ã®éèŠãªéãããããŸããçŽæ¥ã¢ã¯ã»ã¹ããããšã¯ã§ããŸããã ããããã£ãå€æŽã§ããŸããããªããžã§ã¯ãèªäœã«ã¯ã¢ã¯ã»ã¹ã§ããŸããã
ãã®ãããªã¹ã³ãŒãã®ãªããžã§ã¯ãã®æŠå¿µã¯ãããŒã«ã«å€æ°ãã¹ã¿ãã¯ã«æ ŒçŽãããCãC ++ãªã©ãšã¯å€§ããç°ãªããŸãã JavaScriptã§ã¯ããã®ãããªãªããžã§ã¯ãã¯ããŒãã«æ ŒçŽãããé¢æ°ãå€ãè¿ããåŸã§ãã¡ã¢ãªã«æ®ãããšãã§ããŸãã ããã«ã€ããŠã¯åŸã§èª¬æããŸãã
ãæ³åã®ãšãããã¹ã³ãŒããªããžã§ã¯ãã«ã¯èŠªããããŸãã ã³ãŒããå€æ°ã«ã¢ã¯ã»ã¹ããããšãããšãã€ã³ã¿ãŒããªã¿ãŒã¯çŸåšã®ã¹ã³ãŒããªããžã§ã¯ãã®ããããã£ãæ€çŽ¢ããŸãã ããããã£ãååšããªãå Žåãã€ã³ã¿ãŒããªã¿ãŒã¯ã¹ã³ãŒããªããžã§ã¯ãã®ãã§ãŒã³ãäžã«ç§»åããæ€çŽ¢ãç¶è¡ããŸãã ãªã©ãããããã£ãèŠã€ãããŸã§ããŸãã¯èŠªããªããªããŸã§ã ãã®ã¹ã³ãŒãã®é åºããã¹ã³ãŒããã§ãŒã³ããŸãã¯ãã¹ã³ãŒããã§ãŒã³ããšåŒã³ãŸãããã
ãã®ã¡ã«ããºã ã¯ãããã¿ã€ãã®ç¶æ¿ã«éåžžã«äŒŒãŠããŸãããããã§ãéèŠãªéãã1ã€ãããŸããéåžžã®ãªããžã§ã¯ãã®ååšããªãããããã£ã«ã¢ã¯ã»ã¹ããããšããŠããããã¿ã€ããã§ãŒã³å ã«ãã®ãããªããããã£ããªãå Žåãããã¯ãšã©ãŒã§ã¯ãªããåã«æªå®çŸ©ãè¿ããŸãã ãã ããã¹ã³ãŒããã§ãŒã³å ã«ååšããªãããããã£ãæå¹ã«ããïŒã€ãŸããååšããªãå€æ°ãæå¹ã«ããïŒãšãReferenceErrorãšã©ãŒãçºçããŸãã
ã¹ã³ãŒããã§ãŒã³ã®æåŸã®èŠçŽ ã¯åžžã«ã°ããŒãã«ãªããžã§ã¯ãã§ãã æé«ã¬ãã«ã®JavaScriptã³ãŒãã§ã¯ããªããžã§ã¯ãã®ã¹ã³ãŒããã§ãŒã³ã¯ãã°ããŒãã«ãªããžã§ã¯ããšãã1ã€ã®èŠçŽ ã®ã¿ã§æ§æãããŸãã ãããã£ãŠãã³ãŒãã®ãããã¬ãã«ã§å€æ°ãäœæãããšããããã¯ã°ããŒãã«ãªããžã§ã¯ãã«èšå®ãããŸãã é¢æ°ãåŒã³åºããããšãã¹ã³ãŒããã§ãŒã³ã«è€æ°ã®ãªããžã§ã¯ãããããŸãã é¢æ°ããããã¬ãã«ããåŒã³åºãããå Žåãã¹ã³ãŒããã§ãŒã³ã«ã¯ã¹ã³ãŒãã®ãªããžã§ã¯ããã¡ããã©2ã€ãããšæããããããŸããããåžžã«ããã§ãããšã¯éããŸããã 2ã€ä»¥äžã®ãªããžã§ã¯ããååšããå ŽåããããŸãããæ©èœã«ãã£ãŠç°ãªããŸãã ããã«ã€ããŠã¯åŸã§è©³ãã説æããŸãã
ãããã¬ãã«
ååãªçè«ãããã«äŸããããŸãïŒ
my_script.js
"use strict"; var foo = 1; var bar = 2;
ãããã¬ãã«ã§2ã€ã®å€æ°ãäœæããŸããã äžã§èª¬æããããã«ããã®å Žåãã¹ã³ãŒããªããžã§ã¯ãã¯ã°ããŒãã«ãªããžã§ã¯ãã§ãã
ããã«ã¯ãèµ·åãšãªã¢ïŒããã¯my_script.jsã®ãããã¬ãã«ã³ãŒãã§ãïŒãšå¯Ÿå¿ããã¹ã³ãŒããªããžã§ã¯ãããããŸãã ãã¡ãããå®éã«ã¯ãã°ããŒãã«ãªããžã§ã¯ãã«ã¯ãŸã å€ãã®æšæºããã³ãã¹ãåºæã®ããŒã¹ãå«ãŸããŠããŸãããããã§ã¯ãããã衚瀺ããŸããã
ãã¹ããããŠããªãé¢æ°
ãã®ã¹ã¯ãªãããèŠãŠãã ããïŒ
my_script.js
"use strict"; var foo = 1; var bar = 2; function myFunc() { //-- , var a = 1; var b = 2; var foo = 3; console.log("inside myFunc"); } console.log("outside"); //-- myFunc();
myFuncé¢æ°ãå®çŸ©ããããšãmyFuncèå¥åãçŸåšã®ã¹ã³ãŒããªããžã§ã¯ãïŒãã®å Žåã¯ã°ããŒãã«ãªããžã§ã¯ãïŒã«è¿œå ããããã®èå¥åã¯é¢æ°ãåç §ããŸãã ãåç¥ã®ããã«ãé¢æ°ã¯ãªããžã§ã¯ãã§ãããããã£ãŠããé¢æ°ãªããžã§ã¯ãããšèšããšãããªããžã§ã¯ããæå³ããŸãããããé¢æ°ã§ãã
é¢æ°ãªããžã§ã¯ãã«ã¯ãé¢æ°ã³ãŒããšãã®ä»ã®ããããã£ãå«ãŸããŸãã é¢å¿ã®ããããããã£ã®1ã€ã¯ãå éš[[scope]]ããããã£ã§ãã ããã¯ãçŸåšã®ã¹ã³ãŒããªããžã§ã¯ããã€ãŸããé¢æ°ãå®çŸ©ããããšãã«ã¢ã¯ãã£ãã«ãªã£ãŠããã¹ã³ãŒããªããžã§ã¯ããåç §ããŸãïŒãã®å Žåãã°ããŒãã«ãªããžã§ã¯ãã§ãïŒã
console.logïŒãå€éšãïŒãåŒã³åºããšã次ã®ã¹ããŒã ãåŸãããŸãã
myFuncå€æ°ã«ãã£ãŠåç §ããããªããžã§ã¯ãé¢æ°ã¯ãé¢æ°ã³ãŒããæ ŒçŽããé¢æ°ãå®çŸ©ããããšãã«é¢é£ããŠããã¹ã³ãŒããªããžã§ã¯ããåç §ããŸãã ããã¯éåžžã«éèŠã§ãã
é¢æ°ãåŒã³åºããããšãmyFuncã®ããŒã«ã«å€æ°ïŒããã³ãã®åŒæ°ã®å€ïŒãæ ŒçŽããæ°ããã¹ã³ãŒããªããžã§ã¯ããäœæããããã®æ°ããã¹ã³ãŒããªããžã§ã¯ãã¯ãåŒã³åºãããé¢æ°ãåç §ããã¹ã³ãŒããªããžã§ã¯ãããç¶æ¿ããŸãã
ãããã£ãŠãmyFuncãåŒã³åºããšãåè·¯ã¯æ¬¡ã®ããã«ãªããŸãã
ããã¯ãã¹ã³ãŒããªããžã§ã¯ãã®ãã§ãŒã³ã§ãã myFuncå ã®å€æ°ãèŠããšãJavaScriptã¯ãã§ãŒã³ã®æåã®ãªããžã§ã¯ã-é¢æ°myFuncïŒïŒã®ã¹ã³ãŒãã§ãã®å€æ°ãèŠã€ããããšããŸãã ãã®ãããªå€æ°ãååšããªãå Žåã¯ãäžäœã«ç§»åããå¿ èŠããããŸãïŒãã®å Žåãã°ããŒãã«ãªããžã§ã¯ãã¯äžäœã«ãªããŸãïŒã ãã§ãŒã³å šäœã§äœãèŠã€ãããªãå ŽåãReferenceErrorãšã©ãŒãçºçããŸãã
ããšãã°ãå åŽã®myFuncã«ç®ãåãããšãæåã®ãªããžã§ã¯ãïŒã¹ã³ãŒããªããžã§ã¯ãmyFuncïŒïŒïŒãã1ãååŸããŸãã fooã«æ»ããšãåããªããžã§ã¯ããã3ãååŸããŸããã°ããŒãã«ãªããžã§ã¯ãã®fooããããã£ãé ããŠãããšèšããŸãã barã«æ»ããšãã°ããŒãã«ãªããžã§ã¯ããã2ãååŸããŸãã ããã¯ããããã¿ã€ãç¶æ¿ã®ããã«æ©èœããŸãã
ãããã®ã¹ã³ãŒããªããžã§ã¯ãã¯ãåç §ãããŠããéãååšãç¶ããããšã«æ³šæããŠãã ããã ãã®ãããªãªããžã§ã¯ããžã®æåŸã®åç §ããªããªããšããªããžã§ã¯ãã¯ã¬ããŒãžã³ã¬ã¯ã¿ãŒã«ãã£ãŠåŠçãããŸãã
myFuncïŒïŒãæ»ã£ãåŸãmyFuncïŒïŒã®ã¹ã³ãŒããžã®åç §ã¯ãªããªããã¬ããŒãžã³ã¬ã¯ã¿ãŒã¯ãã®åœ¹å²ãæããã次ã®ããã«ãªããŸãã
ããã«ãã€ã©ã¹ãããªãŒããŒããŒãããªãããã«ããã€ã¢ã°ã©ã ã«é¢æ°ãªããžã§ã¯ããå«ããŸããã æ¢ã«ãåãã®ããã«ããã§ãŒã³ã¯æ¬¡ã®ããã«ãªããŸãïŒé¢æ°âé¢æ°ãªããžã§ã¯ãâã¹ã³ãŒããªããžã§ã¯ãã
ãããå¿ããªãã§ãã ããã
å ¥ãåé¢æ°
é¢æ°ãå€ãè¿ãç¬éããã誰ããã®ã¹ã³ãŒããªããžã§ã¯ãã«ã¢ã¯ã»ã¹ããªããããã¬ããŒãžã³ã¬ã¯ã¿ãŒã«ãã£ãŠåéãããŸãã ãããããã¹ããããé¢æ°ãå®çŸ©ããŠè¿ãïŒãŸãã¯çŸåšã®ã¹ã³ãŒããªããžã§ã¯ãã®å€éšã«ä¿åããïŒå Žåãçãã¯æ¢ã«ããã£ãŠããŸããé¢æ°ãªããžã§ã¯ãã¯åžžã«ãäœæãããã¹ã³ãŒããªããžã§ã¯ããåç §ããŸãã ãããã£ãŠããã¹ããããé¢æ°ãå®çŸ©ãããšãå€éšé¢æ°ã®çŸåšã®ã¹ã³ãŒããžã®ãªã³ã¯ãååŸãããŸãã ãŸãããã¹ããããé¢æ°ãå¥ã®å Žæã«ä¿åãããšãå€éšé¢æ°ãå€ãè¿ããŠããã¹ã³ãŒããªããžã§ã¯ãã¯ã¬ããŒãžã³ã¬ã¯ã¿ãŒã«ãã£ãŠåŠçãããŸãããçµå±ããã®ã¹ã³ãŒããªããžã§ã¯ããžã®ãªã³ã¯ãæ®ã£ãŠããŸãã ãã®ã³ãŒããèŠãŠãã ããïŒ
my_script.js
"use strict"; function createCounter(initial) { //-- , var counter = initial; //-- . // scope- ( ) /** * . * 1 â 1. */ function increment(value) { if (!isFinite(value) || value < 1){ value = 1; } counter += value; } /** * . */ function get() { return counter; } //-- , // return { increment: increment, get: get }; } //-- var myCounter = createCounter(100); console.log(myCounter.get()); //-- "100" myCounter.increment(5); console.log(myCounter.get()); //-- "105"
createCounterïŒ100ïŒãåŒã³åºããšã; ãã®ã¹ããŒã ãå€æããŸãïŒ
å¢åããã³createãã¹ãé¢æ°ããcreateCounterïŒ100ïŒã®ã¹ã³ãŒããžã®åç §ãããããšã«æ³šæããŠãã ããã createCounterïŒïŒãäœãè¿ããªãå Žåããã¡ãããããããžã®å éšåç §ã¯èæ ®ããããã¹ã³ãŒããªããžã§ã¯ãã¯ã¬ããŒãžã³ã¬ã¯ã¿ãŒã«ãã£ãŠåéãããŸãã ãã ããcreateCounterïŒïŒã¯ãããã®é¢æ°ãžã®ãªã³ã¯ãæã€ãªããžã§ã¯ããè¿ãããã次ã®ããã«ãªããŸãã
ãã®ãããcreateCounterïŒ100ïŒé¢æ°ã¯ãã§ã«å€ãè¿ããŠããŸããããã®ã¹ã³ãŒãã¯ãŸã ååšããå éšé¢æ°ããã®ã¿ã¢ã¯ã»ã¹ã§ããŸãã createCounterïŒ100ïŒã®ã¹ã³ãŒãã«çŽæ¥ã¢ã¯ã»ã¹ããæ¹æ³ã¯ãªããmyCounter.incrementïŒïŒãŸãã¯myCounter.getïŒïŒã®ã¿ãåŒã³åºãããšãã§ããŸãã ãããã®é¢æ°ã«ã¯ãcreateCounterãšãªã¢ãžã®äžæã®ãã©ã€ããŒãã¢ã¯ã»ã¹ããããŸãã
myCounter.getïŒïŒãåŒã³åºããŠã¿ãŸãããã èŠç¢ºèª-é¢æ°ãåŒã³åºããããšãæ°ããã¹ã³ãŒããäœæããããã®æ°ããé¢æ°ã«äœ¿çšãããã¹ã³ãŒããã§ãŒã³ã«æ°ãããªããžã§ã¯ããè¿œå ãããŸãã 次ã®ããã«ãªããŸãã
getïŒïŒé¢æ°ãã§ãŒã³ã®æåã®ã¹ã³ãŒããªããžã§ã¯ãã¯ãé¢æ°èªäœã®ç©ºã®ã¹ã³ãŒããªããžã§ã¯ãã§ãã getïŒïŒå ã§ã«ãŠã³ã¿ãŒã«ã¢ã¯ã»ã¹ãããšãJavaScriptã¯ãã§ãŒã³ã®æåã®ãªããžã§ã¯ãã§äœãæ€åºã§ããã次ã®ãªããžã§ã¯ãã«ç§»åããcreateCounterïŒ100ïŒã®ã¹ã³ãŒãã§ã«ãŠã³ã¿ãŒã䜿çšããŸãã ãããŠãgetïŒïŒé¢æ°ã¯åã«ãããè¿ããŸãã
myCounterãªããžã§ã¯ãã¯ãmyCounter.getïŒïŒé¢æ°ã§ããthisãïŒå³ã®èµ€ãç¢å°ïŒãšããŠå©çšã§ããããšã«æ°ä»ããããããŸããã ããã¯ã¹ã³ãŒããã§ãŒã³ã®äžéšã§ã¯ãããŸãããã泚æããå¿ èŠããããŸãã ããã«ã€ããŠã¯åŸã§è©³ãã説æããŸãã
åŒæ°ããããããã€ã³ã¯ãªã¡ã³ãïŒ5ïŒåŒã³åºãã¯ããå°ãèå³æ·±ãã§ãïŒ
åŒæ°ã®å€ã¯ããã®åŒã³åºãçšã«äœæãããã¹ã³ãŒããªããžã§ã¯ãã«æ ŒçŽãããŸãã é¢æ°ãå€æ°ã®å€ã«ã¢ã¯ã»ã¹ãããšãJavaScriptã¯ãã§ãŒã³ã®æåã®ãªããžã§ã¯ãã§ããã«ãããèŠã€ããŸãã ãã ããé¢æ°ãã«ãŠã³ã¿ãŒã«ã¢ã¯ã»ã¹ãããšãJavaScriptã¯ã¹ã³ãŒããã§ãŒã³ã®æåã®ãªããžã§ã¯ãã§ãããèŠã€ããããšãã§ãããäžäœã«ç§»åããŠããã«èŠã€ããŸãã ãããã£ãŠãincrementïŒïŒã¯ãcreateCounterïŒ100ïŒã®ã¹ã³ãŒãå ã®ã«ãŠã³ã¿ãŒã®å€ãå€æŽããŸãã ãããŠäºå®äžããã®å€ãå€æŽã§ãããã®ã¯ä»ã«ãããŸããã ãããã£ãŠãã¯ããŒãžã£ãŒã¯éåžžã«éèŠã§ããmyCounterãªããžã§ã¯ããéãããšãã§ããŸããã ã¯ããŒãžã£ãŒã¯ãæ©å¯æ å ±ãä¿æããã®ã«é©ããŠããŸãã
ã¹ã³ãŒãããã©ã€ããã§ããããšãç解ããããšãéèŠã§ãã é¢æ°ãåŒã³åºããããšãçŸåšã®ãã§ãŒã³ã¯é¢æ°ã«å¯ŸããŠã³ããŒãããŸããããå®éã«ã¯æ°ãããªããžã§ã¯ãã§è£è¶³ãããŸãã ãŸãããã§ãŒã³å ã®ãªããžã§ã¯ããå€æŽããããšããã®å€æŽã¯ããã®ãªããžã§ã¯ããå«ãŸãããã§ãŒã³å ã®ãã¹ãŠã®é¢æ°ã§ããã«äœ¿çšå¯èœã«ãªããŸãã incrementïŒïŒãã«ãŠã³ã¿ãŒã®å€ãå€æŽããåŸãgetïŒïŒã®æ¬¡ã®åŒã³åºãã¯æŽæ°ãããå€ãè¿ããŸãã
ãããã£ãŠããã®æåãªäŸã¯æ©èœããŸããã
"use strict"; var elems = document.getElementsByClassName("myClass"), i; for (i = 0; i < elems.length; i++) { elems[i].addEventListener("click", function () { this.innerHTML = i; }); }
ã«ãŒãå ã«ããã€ãã®é¢æ°ãäœæããããããã®ãã¹ãŠã«åãã¹ã³ãŒããªããžã§ã¯ããžã®åç §ãå«ãŸããŠããŸãã ãããã£ãŠãå人ã®ã³ããŒã§ã¯ãªããåãå€æ°iã䜿çšããŸãã ãªã³ã¯ã§ãã®äŸã®è©³çŽ°ãèªãã§ãã ãã ã ã«ãŒãå ã§é¢æ°ãäœæããªãã§ãã ãã ã
åæ§ã®é¢æ°ãªããžã§ã¯ããç°ãªãã¹ã³ãŒããªããžã§ã¯ã
ããã§ã¯ãäŸãå°ãæ¡åŒµããŠæ¥œããã§ã¿ãŸãããïŒ ã¯ããç§ã¯æ¥œããã§ããŸã-çŽPerã ïŒã è€æ°ã®ã«ãŠã³ã¿ãŒãªããžã§ã¯ããäœæãããšã©ããªããŸããïŒ
my_script.js
"use strict"; function createCounter(initial) { /* ... . ... */ } //-- var myCounter1 = createCounter(100); var myCounter2 = createCounter(200);
myCounter1ãšmyCounter2ãäœæããåŸã次ã®ã¹ããŒã ãååŸããŸãã
èŠç¢ºèªïŒåé¢æ°ãªããžã§ã¯ãã«ã¯ãã¹ã³ãŒããªããžã§ã¯ããžã®åç §ãå«ãŸããŠããŸãã ãã®äŸã§ã¯ãmyCounter1.incrementãšmyCounter2.incrementã¯ãåãã³ãŒããšåãããããã£å€ïŒååãé·ããªã© ïŒãå«ãé¢æ°ãªããžã§ã¯ããåç §ããŠããŸããã[[scope]]ã¯ç°ãªãã¹ã³ãŒããåç §ããŠããŸããªããžã§ã¯ã ã
ïŒèŠèŠåãç°¡åã«ããããã«ïŒãã€ã¢ã°ã©ã ã«ã¯åå¥ã®é¢æ°ãªããžã§ã¯ãã¯ãããŸãããããããã¯ãŸã ååšããŠããŸãã
äŸïŒ
var a, b; a = myCounter1.get(); // a == 100 b = myCounter2.get(); // b == 200 myCounter1.increment(1); myCounter1.increment(2); myCounter2.increment(5); a = myCounter1.get(); // a == 103 b = myCounter2.get(); // b == 205
ããããã®ä»çµã¿ã§ãã ééã®æŠå¿µã¯åã§ãã
ã¹ã³ãŒããªããžã§ã¯ãã®ãã§ãŒã³ãšãã
奜ããšå¥œãŸãããšã«ããããããããã¯ã¹ã³ãŒããã§ãŒã³ã®äžéšã§ã¯ãããŸããã ãã®å€ã¯ãé¢æ°åŒã³åºããã¿ãŒã³ã«äŸåããŸãã ã€ãŸããåãé¢æ°ãåŒã³åºãããšãã§ããŸãããå éšã§ã¯ãã®å€ãç°ãªããŸãã
åŒã³åºããã¿ãŒã³
ãã®ããŒãã«ã€ããŠã¯å¥ã®èšäºãæžã䟡å€ãããã®ã§ãããã§ã¯è¡šé¢çã«ãããã¯ã説æããŸãã 4ã€ã®ãã¿ãŒã³ããããŸãã ããã«ïŒ
ã¡ãœããåŒã³åºããã¿ãŒã³
"use strict"; var myObj = { myProp: 100, myFunc: function myFunc() { return this.myProp; } }; myObj.myFunc(); //-- 100
åŒã³åºãã«ããªãªããŸãã¯[æ·»ãå]ãå«ãŸããŠããå Žåãé¢æ°ã¯ã¡ãœãããšããŠåŒã³åºãããŸãã äžèšã®äŸã§ã¯ãããã¯myObjãæããŸãã
é¢æ°åŒã³åºããã¿ãŒã³
"use strict"; function myFunc() { return this; } myFunc(); //-- undefined
ãã®å Žåããã®å€ã¯ã³ãŒããã¹ããªã¯ãã¢ãŒãã§å®è¡ãããŠãããã©ããã«ãã£ãŠç°ãªããŸãã
- å³æ Œã¢ãŒãã§ã¯ãããã¯æªå®çŸ©ã§ã
- éå³å¯ã¢ãŒãã§ã¯ãããã¯ã°ããŒãã«ãªããžã§ã¯ããæããŸãã
äžèšã®äŸã§ã¯ãå³æ Œã¢ãŒãã§ãããããmyFuncïŒïŒã¯æªå®çŸ©ãè¿ããŸãã
ã³ã³ã¹ãã©ã¯ã¿ãŒåŒã³åºããã¿ãŒã³
"use strict"; function MyObj() { this.a = 'a'; this.b = 'b'; } var myObj = new MyObj();
é¢æ°ããã¬ãã£ãã¯ã¹newã§åŒã³åºããããšãJavaScriptã¯æå®ãããé¢æ°ã®prototypeããããã£ããç¶æ¿ããæ°ãããªããžã§ã¯ããèšå®ããŸãã ãããŠããã®æ°ããäœæããããªããžã§ã¯ãã¯ããã®ããã«é¢æ°ã«æž¡ãããŸãã
åŒã³åºããã¿ãŒã³ãé©çšãã
"use strict"; function myFunc(myArg) { return this.myProp + " " + myArg; } var result = myFunc.apply( { myProp: "prop" }, [ "arg" ] ); //-- â "prop arg"
ãã®ãããªä»»æã®å€ãæž¡ãããšãã§ããŸãã ãã®äŸã§ã¯ãFunction.prototype.applyïŒïŒãããã«äœ¿çšãããŸãã ãã®ä»ã®ãªãã·ã§ã³ïŒ
- Function.prototype.callïŒïŒ
- Function.prototype.bindïŒïŒ
次ã®äŸã§ã¯ãäž»ã«ã¡ãœããåŒã³åºããã¿ãŒã³ã䜿çšããŠããŸãã
ãã¹ããããé¢æ°ã§ããã䜿çšãã
"use strict"; var myObj = { myProp: "outer-value", createInnerObj: function createInnerObj() { var hidden = "value-in-closure"; return { myProp: "inner-value", innerFunc: function innerFunc() { return "hidden: '" + hidden + "', myProp: '" + this.myProp + "'"; } }; } }; var myInnerObj = myObj.createInnerObj(); console.log( myInnerObj.innerFunc() );
åºåïŒhiddenïŒ 'value-in-closure'ãmyPropïŒ 'inner-value'
myObj.createInnerObjïŒïŒãåŒã³åºããããŸã§ã«ã次ã®æ§é ãååŸãããŸãã
ãããŠãmyInnerObj.innerFuncïŒïŒãåŒã³åºããããŸã§ã«-ãã®ããã«ïŒ
myObj.createInnerObjïŒïŒã®ããã¯myObjãæããŸãããmyInnerObj.innerFuncïŒïŒã®ããã¯myInnerObjãæããŸããäž¡æ¹ã®é¢æ°ã¯ã¡ãœãããšããŠåŒã³åºãããŸãã ãããã£ãŠãinnerFuncïŒïŒå ã®this.myPropã¯ãå€éšå€ã§ã¯ãªãå éšå€ãè¿ããŸãã
innerFuncïŒïŒãã ãŸããŠmyPropã次ã®ããã«äœ¿çšã§ããŸãã
var myInnerObj = myObj.createInnerObj(); var fakeObject = { myProp: "fake-inner-value", innerFunc: myInnerObj.innerFunc }; console.log( fakeObject.innerFunc() );
åºåïŒhiddenïŒ 'value-in-closure'ãmyPropïŒ 'fake-inner-value'
ãŸãã¯ãapplyïŒïŒãŸãã¯callïŒïŒã§ïŒ
var myInnerObj = myObj.createInnerObj(); console.log( myInnerObj.innerFunc.call( { myProp: "fake-inner-value-2", } ) );
åºåïŒhiddenïŒ 'value-in-closure'ãmyPropïŒ 'fake-inner-value-2'
ãã ããå éšé¢æ°ãå®éã«ã¢ã¯ã»ã¹ããå¿ èŠãããå Žåããããå éšé¢æ°ã®åŒã³åºãæ¹æ³ã«é¢ä¿ãªããå€éšé¢æ°ã§äœ¿çšã§ããŸãã ãããè¡ãã«ã¯ã次ã®ããã«ãã¯ããŒãžã£ãŒïŒã€ãŸããçŸåšã®ã¹ã³ãŒããªããžã§ã¯ãïŒã«ç®çã®å€ãå ·äœçã«ä¿åããå¿ èŠããããŸããvar self = this; ãããŠãããã®ä»£ããã«å éšé¢æ°ã§selfã䜿çšããŸãã
"use strict"; var myObj = { myProp: "outer-value", createInnerObj: function createInnerObj() { var self = this; var hidden = "value-in-closure"; return { myProp: "inner-value", innerFunc: function innerFunc() { return "hidden: '" + hidden + "', myProp: '" + self.myProp + "'"; } }; } }; var myInnerObj = myObj.createInnerObj(); console.log( myInnerObj.innerFunc() );
åºåïŒhiddenïŒ 'value-in-closure'ãmyPropïŒ 'outer-value'
次ã®ããã«ãªããŸãã
ããã§ãinnerFuncïŒïŒã¯ã¯ããŒãžã£ãŒã«ããselfãä»ããŠãã®å€éšé¢æ°ã®å€ã«ã¢ã¯ã»ã¹ã§ããããšãããããŸãã
ãããã«
ããã§ãæåã®æ®µèœãããããã®è³ªåã«çããããšãã§ããŸãã
ééãšã¯äœã§ããïŒ ããã¯ãé¢æ°ãªããžã§ã¯ããšã¹ã³ãŒããªããžã§ã¯ãã®äž¡æ¹ã«é¢é£ä»ãããããªããžã§ã¯ãã§ãã å®éãJavaScriptã®ãã¹ãŠã®é¢æ°ã¯ã¯ããŒãžã£ãŒã§ããã¹ã³ãŒããªããžã§ã¯ããªãã§é¢æ°ãªããžã§ã¯ããžã®åç §ãæã€ããšã¯äžå¯èœã§ãã
ã¯ããŒãžã£ãŒã¯ãã€äœæãããŸããïŒ JavaScriptã®ãã¹ãŠã®é¢æ°ã¯ã¯ããŒãžã£ãŒãªã®ã§ãçãã¯æããã§ããé¢æ°ãèšå®ããããšã¯ããŒãžã£ãŒãèšå®ãããŸãã ãããã£ãŠãé¢æ°ãå®çŸ©ããããšã¯ããŒãžã£ãŒãäœæãããŸãã ãã ããã¯ããŒãžã£ã®äœæãšæ°ããã¹ã³ãŒããªããžã§ã¯ãã®äœæã®éããç解ããå¿ èŠããããŸããé¢æ°ãå®çŸ©ããããšã¯ããŒãžã£ïŒé¢æ°+ã¹ã³ãŒããªããžã§ã¯ãã®çŸåšã®ãã§ãŒã³ãžã®åç §ïŒãäœæãããŸãããåŒã³åºããããšæ°ããã¹ã³ãŒããªããžã§ã¯ããäœæãããŸãïŒã¯ããŒãžã£ã¹ã³ãŒããªããžã§ã¯ãã®ãã§ãŒã³ãå€æŽããããã«äœ¿çšãããŸãïŒæ©èœã
ã¯ããŒãžã£ã¯ãã€ç Žå£ãããŸããïŒ JavaScriptã®ä»ã®ãªããžã§ã¯ããšåæ§ã«ãã¬ããŒãžã³ã¬ã¯ã¿ãŒã¯ãããã«å¯Ÿããåç §ããªããªã£ããšãã«ã¯ããŒãžã£ãŒãåŠçããŸãã