ç§ãä»ã¾ã§ã¯ãã¼ã¸ã£ãç解ã§ããªãã£ãçç±
Javascriptãåå¼·ããä¸ã§èª°ããã¤ã¾ããã¨è¨ãããã¯ãã¼ã¸ã£ã§ããç§ãä¾ã«æ¼ããç解ã§ããªãã§ãã¾ããã
ãã¾ãã¾ãªè§£èª¬ãµã¤ããèªãã ããµã³ãã«ã³ã¼ããæ¸ãæãã¦ã¿ãããã¦
- éçãªã¹ã³ã¼ãã®è¨èªã§å©ç¨ã§ããã
- é¢æ°ãçµäºãããã¨ããã®ãã¼ã«ã«å¤æ°ãåç §ã§ããã
ã¨ãã£ãã¯ãã¼ã¸ã£ã®ãä»çµã¿ãããç¹å¾´ãã«ã¤ãã¦ã¯åãã£ããã®ã®ããã£ã¨ãéè¦ã¨ãããã
ãã©ãããæã«ã¯ãã¼ã¸ã£ã使ãã°ããã®ãã
ãåããã¾ããã§ãããè¨ããããã¨å人ããªãã¦è¨ã£ã¦æ©ãã§ããæã«
ãããè¨ãæã¯ã¯ãã¼ã¸ã£ã使ãã¨ãããã
ã¨è¨ã£ã¦ãããã°ããã®ãã
ä¾ãã°ããã°ã©ã ã®åå¼·ãå§ããå人ã
ãããã¨åãå¦çããä½åãæ¸ãã¦ããã ããã³ããã°ã£ãããã¦ãæ°ãããã
ã¨è¨ã£ã¦æ©ãã§ããã
ããããé¢æ°ã«ããã°ããããããªãï¼ã
ã£ã¦æãã¦ããã¾ãããã
ããã¨åãããã«å人ã
ãââââââãªãã ãããã¾ãæ¹æ³ãªãï¼ã
ã¨è³ªåãã¦æ¥ãæã«
ãããããæã¯ã¯ãã¼ã¸ã£ã使ãã°ãããã
ã¨çãã¦ãããã¹ãââââââã¯ä½ãªã®ãã
ãããåãã£ã¦åãã¦èªåã§ãã¯ãã¼ã¸ã£ã使ããã¯ãã§ãã
æè¿ã«ãªã£ã¦ããããç§ããã®ã¯ãã¼ã¸ã£ãã©ãããæã«ä½¿ãã¹ãããåãã£ã¦ãã¾ããã(ã¨èªåã§ã¯æã£ã¦ã)
ããã«ä¼´ãããªãä»ã¾ã§åãããªãã£ãããã¨ããçç±ãåãã£ã¦ããã®ã§æ´çãããã¨æãã¾ãã
çµè«ããè¨ãã°
ã(ã¯ãã¼ã¸ã£ã使ããã¨ã§)é¢æ°çµäºå¾ããã®ãã¼ã«ã«å¤æ°ãåç
§ã§ãããã¨ããå¾ãããï¼ã¤ã®å¤§ããªã¡ãªããã®å
ãä¸æ¹ãåãã£ã¦ããªãã£ãããã
ã§ã¯ãªããã¨èãã¦ãã¾ãã
é¢æ°çµäºå¾ããã¼ã«ã«å¤æ°ãåç §ã§ãããã¨ã®ã¡ãªããï¼ãã°ãã¼ãã«å¤æ°ã®ç¯æ¸
解説æ¸ãªã©ã§ãã示ããã¦ããã¯ãã¼ã¸ã£ã®ãµã³ãã«ããèªèº«ãå¼ã°ããåæ°ãã«ã¦ã³ãããé¢æ°ãã§ãã
ã¯ã¦ãªã®クロージャとは - はてなキーワードã§ã
builderã®JavaScriptクロージャを完全理解!スコープチェインを知る(後編) - page2 - builder by ZDNet Japanã§ã
ã»ã¼åããµã³ãã«ã示ããã¦ãã¾ãã
///ã³ã¼ãï¼ã(ä¸ã®ã¢ãã¬ã¹ã®ãã¼ã¸ããå¼ç¨) // é¢æ°ãªãã¸ã§ã¯ãããªã¿ã¼ã³ããé¢æ° function createCounter() { var n = 0; // ã¯ãã¼ã¸ã£ãä½æãã¦è¿ã return function() { return n++; }; } // createCounter()ãå¼ã³åºãã // æ»ãå¤ (é¢æ°ãªãã¸ã§ã¯ã) ãå¤æ°ã«æ ¼ç´ãã¦ãã var counter = createCounter(); alert(counter()); // 0ã表示ããã alert(counter()); // 1ã表示ããã ...http://builder.japan.zdnet.com/script/sp_javascript-kickstart-2007/20378258/2/
åãæ©è½ãã¯ãã¼ã¸ã£ã使ããã«æ¸ãã¨ä¸ã®ããã«ãªãã§ããããã
///ã³ã¼ãï¼ var count = 0; var counter = function(){ return count++; } alert(counter()); //0ã表示ããã alert(counter()); //1ã表示ããã
両è ã®éãã¯ä½ãã¨è¨ãã¨ã使ç¨ããªããã°ãªããªãã°ãã¼ãã«å¤æ°ã®æ°ãã§ããä¸ã®å¼ç¨ã®ä¾ã§ã¯creatCounterã¨ããååã¨counterã¨ããååã®2ã¤ã使ããã¦ãã¾ããcreatCounterã®æ¹ã¯ç¡åé¢æ°åãã¦
///ã³ã¼ãï¼ var counter = (function(){ var n = 0; // ã¯ãã¼ã¸ã£ãä½æãã¦è¿ã return function() { return n++; }; })(); alert(counter()); // 0ã表示ããã alert(counter()); // 1ã表示ããã
ã¨ãæ¸ããã¨ãã§ããããã©ããã¦ã使ç¨ãé¿ããããªãã°ãã¼ãã«å¤æ°ã®æ°ã¯å®è³ª1ã¤ã§ãã
ããã«å¯¾ãã¦ã³ã¼ãï¼ã®ä¾ã§ã¯countã¨ããå¤æ°ã¨counterã¨ããå¤æ°ã¯ã©ã¡ããå¿
ã使ããããå¾ã¾ãããcounterã¯ãã¡ããã®ãã¨countã®æ¹ãèå¿ã®å¼ã°ããåæ°ãä¿åãã¦ããããã§ãã
ãããããªãã°ãã¼ãã«å¤æ°ãå°ãªãæ¹ããããã¨ãã話ã¯æ¬é¡ãããããã®ã§é¿ãã¾ããã¯ãã¼ã¸ã£ã®ã¡ãªããã®ï¼ã¤ããã®ãã¨ã§ããäºã¯ã¾ã¡ããããã¾ããã
ããã¦ããã¾ã§ã¯ã¯ãã¼ã¸ã£ã説æãããµã³ãã«ã³ã¼ãã§ãã¾ãééããªãããããã¦ãã¾ãã
ãã¦ãããããæ¬é¡ã¨ãããã¨ã«ãªãã¾ãã(ãããã(^_^;))
é¢æ°çµäºå¾ããã¼ã«ã«å¤æ°ãåç §ã§ãããã¨ã®ã¡ãªããï¼ãã¯ãã¼ã¸ã£ã使ãã¨ï¼ã®ã¡ãªããã享åãã¤ã¤è¨ç®éãå¢ãããªããã¨ãã§ãã
ãã¦ãä¸ã®ãèªèº«ãå¼ã°ããåæ°ãã«ã¦ã³ãããé¢æ°ãã§ã¯ã¯ãã¼ã¸ã£ã使ããã¨ã§ã°ãã¼ãã«å¤æ°ãæ¸ãããã¨ãã§ãã¾ããããä¸æ¹ã§è¨ç®éãã®ãã®ã¯å¤åãããã¾ããã§ãããã°ãã¼ãã«å¤æ°ããã¼ã«ã«å¤æ°ãã®å·®ã¯ããã©counter()ãå®è¡ããããã³ã«ããå¤æ°ã+1ãã¦ããã¨ããå¦çã«éãã¯ãªãããã§ãã
ã¯ãã¼ã¸ã£ã®ãé¢æ°çµäºå¾ããã¼ã«ã«å¤æ°ãåç
§ã§ããã¨ããç¹å¾´ãã使ãã¨æ¬¡ã®ããã«æ®éã«æ¸ãã¨è¨ç®éãå¢ãã¦ãã¾ãå ´åã«ããããé²ããã¨ãã§ãã¾ãã
ä»ãlang ã¨ããå¤æ°ã®å¤ã"English"ã§ããã°"Hello"ã"Japanese"ã§ããã°"ããã«ã¡ã¯"ã¨ããæååãã¢ã©ã¼ãããhello()ã¨ããé¢æ°ãèãã¦ã¿ã¾ãã
å
¨ãä½ãèããã«å®è£
ããã°æ¬¡ã®ããã«ãªãã¾ãã
///ã³ã¼ãï¼ var lang = "Japanese"; //ããã§ã¯ãã¿æ¸ãã§ãããã©ã¦ã¶ã®è¨èªè¨å®ãã¿ã¦å¤ãå ¥ãããªã©ããããããã¾ãã var text; //ããã«"Hello"ã¾ãã¯"ããã«ã¡ã¯"ãå ¥ãã if(lang == "English"){ text = "Hello"; } else if(lang == "Japanese"){ text = "ããã«ã¡ã¯"; } function hello(){ window.alert(text); } hello();
lang,text,helloã¨3ã¤ãã°ãã¼ãã«å¤æ°ã使ã£ã¦ãã¾ã£ã¦ãã¾ããlangã¨helloã¯ç¡çã§ããtextã¯ããã«ããã¼ã«ã«å¤æ°ã§ä»£ç¨ã§ãããã§ãã
ã¯ãã¼ã¸ã£ã®åºçªãï¼ï¼ã¨ãªãããã§ããtextããã¼ã«ã«å¤æ°ã«ããã ãã§ããã°ã¯ãã¼ã¸ã£ã使ããªãã¦ã次ã®ããã«æ¸ãã°è¯ãã ãã§ãã
///ã³ã¼ãï¼ var lang = "Japanese"; function hello(){ var text; if (lang == "English") { text = "Hello"; } else if (lang == "Japanese") { text = "ããã«ã¡ã¯"; } window.alert(text); } hello();
è¦äºã«ã°ãã¼ãã«å¤æ°ã¯langã¨helloã®ã¿ã«ãªãã¾ããããããhelloé¢æ°ã®å
å´ã«æ³¨ç®ãã¦ãã ããã
ãã®helloé¢æ°ã¯textããã¼ã«ã«å¤æ°ã«ããããã«é¢æ°ãå¼ã°ãããã³ã«langã®å¤ã"English"ãªã®ã"Japanese"ãªã®ããå¤å¥ããªãã¦ã¯ãªããªããªã£ã¦ãã¾ãã¾ããã
textãã°ãã¼ãã«å¤æ°ã ã£ãã³ã¼ãï¼ã§ã¯langã®ä¸èº«ã確èªããifã®å¦çã¯ä¸åº¦è¡ãã ãã§è¯ãã£ãã®ã«ã°ãã¼ãã«å¤æ°ãæ¸ããã代åã¨ãã¦helloé¢æ°ãå¼ã°ãããã³ã«æ¯åæ¯åifã®å¦çãããªãã¦ã¯ãªããªããªã£ãããã§ãã
ãã¼ãããªãã¨ãã°ãã¼ãã«å¤æ°ã®æ°ãæ¸ããããããifã®å¦çãä¸åº¦ã§ããæ¹æ³ã¯ãªããã®ãã
ããã§ããããã¾ãããããããã¯ãã¼ã¸ã£ã®ç価ã§ãã
///ã³ã¼ãï¼ var lang = "English"; var hello = (function(){ var text; if (lang == "English") { text = "Hello"; } else if (lang == "Japanese") { text = "ããã«ã¡ã¯"; } return function(){ window.alert(text); }; })(); hello();
ã§ãã¾ããï¼ã¯ãã¼ã¸ã£ã®æ¸ãæ¹èªä½ã¯ã³ã¼ãï¼ã®ç¡åé¢æ°ã使ã£ãä¾ã¨åãã§ãã
ã³ã¼ãï¼ã§helloé¢æ°ã®ä¸èº«ã«å®éã«å
¥ã£ã¦ããã®ã¯
function(){ window.alert(text); }
ã¨ããå
容ã ãã§ãããã®textã¯ãã¼ã«ã«å¤æ°ã®textãªã®ã§ä¸åº¦ifã§"Hello"ã¾ãã¯"ããã«ã¡ã¯"ã代å
¥ãããå¾ã¯ãã£ã¨åãå¤ãå
¥ã£ã¦ãã¾ãã
ã¯ãã¼ã¸ã£ã®ãé¢æ°çµäºå¾ããã¼ã«ã«å¤æ°ãåç
§ã§ããç¹å¾´ãã«ãã£ã¦è¦äºã°ãã¼ãã«å¤æ°ãæ¸ãããã¨ã¨ãå¦çãå¢ãããªããã¨ã両ç«ããããã§ãã
ã¾ã¨ã
ãã¦ä¸ã§ããã©ãã¨æ¸ããããã«ã¯ãã¼ã¸ã£ã«é¢ãã¦èª¬æãã¦ãããã¼ã¸ãæ¬ã¯æ°ããã©ãã®å¤ãã¯ï¼ã®ç¹å¾´ã®ã¿ã説æããå ´åãå¤ããã¯ãã¼ã¸ã£ã®ç価ã§ãããï¼ã¨ï¼ã®ä¸¡ç«ã説æããã¦ããæããã¾ããªãããã«æãã¾ããã«ã¦ã³ã¿ã¼ã®ä¾ã§ã¯ãã°ãã¼ãã«å¤æ°ãæ¸ãããå¤ããã«è¨ç®éãå¢ãã¦ãã¾ã£ããã³ã¼ãï¼ã®ãã¿ã¼ã³ãæ³åãã¥ããããï¼ã®ç¹å¾´ã«æ°ã¥ããªããã¨ãè¨ããããããã¾ããã
ç§ã®å ´åãã®ãã¨ããã¾ãã¡ã¯ãã¼ã¸ã£ã®ä½¿ãã©ãããåãããªãã£ãåå ã®ããã«æãã¾ãã
ä¸çªæåã«æ¸ãããå人ããªãã¦è¨ã£ã¦æ©ãã§ããæã«
ãããè¨ãæã¯ã¯ãã¼ã¸ã£ã使ãã¨ãããã
ã¨è¨ã£ã¦ãããã°ããã®ãã
ããã®çãã¯
ãããã£ã¡ã
ãå¼ã³åºãé¢æ°ãªãã ãã©ãã®å¦çã®å
ã®ä¸é¨ã¯å¼ã°ãããã³ã«å
¨ãåããã¨ãããã ããªãã ããããã¨ãã£ã¦ãã®ä¸é¨ãã°ãã¼ãã«ã«ç½®ãã®ã¯å°ããã ããã¾ãæ¹æ³ãªãï¼ã
ã¨ããæãã«ãªãã®ã§ã¯ãªãã§ããããã
â»å¿µã®ããæããå ¥ãã¦ããã¾ããä¸ã§ãªã³ã¯ãã¦ãããµã¤ãã®ä¾ãæªãã¨è¨ã£ã¦ããã®ã§ã¯ããã¾ããããããã¯ç´ãããªããæãç°¡åãªã¯ãã¼ã¸ã£ã®ä¾ãã§ãããä½ãã説æããæã«ãæãç°¡åãªä¾ãã使ããã¨ã¯è³æ¥µå½ç¶ã®ãã¨ã ããã§ãã
以ä¸ãç§ãªãã®ã¯ãã¼ã¸ã£ã«é¢ããæ´çã§ãããããèªãã§ããã ããæ¹ã®ç解ã®ä¸å©ã¨ãªãã°ããã»ã©ãããããã¨ã¯ããã¾ããã