æåãç§ã«ãšã£ãŠJavaScriptã®æãçŽ æŽãããç¬éã®1ã€ã¯æ¬¡ã®ãšããã§ããã
for(var i=0; i<3; i++){ setTimeout(function(){ console.log(i); }, 0); }
èŠãã®ã¯ãããã£ãïŒ
3 3 3
äºæ³ããã代ããã«ïŒ
0 1 2
éå±ãªãšã³ããª
ãã®ãããéåæã¯ããããããããããæåŸ ãããå ŽåïŒããšãã°0ããªç§ïŒãããã³ãåæãã¹ããªãŒã ïŒãããã¯ïŒã®å®è¡ãçµäºãããšããã€ãŸããã§ããã ãæ©ããå®è¡ããããã®ãšããŠèªèãããå¿ èŠããªãããšã«æ°ä»ããŸãããã å®ç掻ã§ã¯ã»ãšãã©ãã¹ãŠã®ããã»ã¹ãéåæã§ãããããããããããé¡æšãããã®ã¯ç°¡åã§ã¯ãããŸããã ããªãã建èšäŒç€Ÿã®ãããŒãžã£ãŒã ãšæ³åããŠãã ããã ã¿ãŒã³ããŒããŠã¹ã®å»ºèšæ³šæãåãåããŸãããããã®ãµã€ãã§ã®ç¹å®ã®çš®é¡ã®äœæ¥ïŒããšãã°ã家ã®å»ºèšïŒã®èš±å¯ãåŸãŠããã®ã¯ãµãŒãããŒãã£äŒæ¥ã®ã¿ã§ããããããã«é£çµ¡ããå¿ èŠããããŸãã ãã§ã«ç¢ºç«ãããã¢ã«ãŽãªãºã ããããŸãïŒåºç€ãåãã家ã建ãŠã家ãå¡ããé°è¬ãç«ãŠããªã©ãããããç§ãã¡ã®ã±ãŒã¹ã§ã¯ã家ã建ãŠãããã€å»ºãŠããããããç¥ããŸããã ããã¯éåæããã»ã¹ã§ãããã¿ã¹ã¯ã¯åã«ã¬ã€ã¢ãŠããäŒç€Ÿã«è»¢éããå®æãã建ç©ãååŸããããšã§ãã 家ã建ãŠããšãã¯ããã¹ãŠãã·ã³ãã«ã§ããã建èšãå¡è£ ãªã©ã®çŸåšã®ããã»ã¹ã«æ³šæãéäžããŸãã ããããä»ã¯å®¶ã建ãŠãŠããŸããã ã©ãããããããç¶æ³ãèæ ®ããŠãããŒã ã®äœæ¥ãæŽçããå¿ èŠããããŸãã ããã¯ãéåæããã»ã¹ã®éãå®è¡ã¹ã¬ããããããã¯ããå¿ èŠããªãçç±ãæããã説æããŸã-ã¹ã¬ããã¯ã¢ã€ãã«ç¶æ ã§ãã å®è¡ã®ã¹ã¬ããããããã¯ãããŠããªãå Žåãéåæã¢ã¯ã·ã§ã³ãçºçããŠããéãä»ã®ããšãè¡ãããšãã§ããŸãã
JavaScriptã«é¢ããŠæã人æ°ã®ããåå¿è ã®ãã°ã¯ã次ã®ããã«ãªããŸãã
function Build(layout){ //... //... , JS ( house) } function paintRoof(house, color){ house.roof.color = color return house; } var layout = {/* - */}, house = {}; Build(layout, house); paintRoof(house, 'red');
æããã«ã TypeErrorãè¿ããundefinedã§roofããããã£ãèªã¿åããªããšèšããŸãã house.foofã¯æªå®çŸ©ã®ãŸãŸã§ãïŒåçŽã«ãã«ãããæéããããŸããã§ããïŒã 家ãåæåããããŸã§äœãšãåŸ ããªããã°ãªãããããããã€èµ·ãããããããŸããã
éå±ãªå°å ¥ããaã³ããŸãã次ã«ããã®èšäºã®ãããã¯ã®ã³ã³ããã¹ãã§JavaScriptã䜿çšããŠãã®åé¡ã解決ããæ¹æ³ã説æããŸãã
ã¢ã€ãã¢
å®éãå€ãã®ããŒã«ã¯ãããŸããã 建èšäŒç€Ÿã®äŸã«æ»ããšããããŒãžã£ãŒãšããŠãããªãã¯ã©ã®ããã»ã¹ãäºãã«äŸåããŠããããç¥ã£ãŠããŸãã 家ã§ã®ãã¹ãŠã®æäœïŒå¡è£ ãå è£ ãªã©ïŒã¯ã家èªäœã建ãŠããããŸã§äžå¯èœã§ãã ãã ããããšãã°ãããŒã«ãæã£ããããã§ã³ã¹ã建ãŠããããã®ä»ã®ã¢ã¯ã·ã§ã³ãå®è¡ããããšã¯éåžžã«å¯èœã§ãã ã¯ãŒã¯ãããŒã調æŽããæ¹æ³ã¯ïŒ æããã«ãè«è² æ¥è ã家ã建ãŠãéãç§ãã¡ã¯ããžãã¹ã«åãæãããŸããã圌ããä»äºãçµãããšããã©ã®ããã«ç¥ããã決ããå¿ èŠããããŸãïŒå®éã®ç掻ã§ã©ãã»ã©ã°ãããŠããããã«èãããŠãïŒã 2ã€ã®ã¢ã€ãã¢ããããŸãã
- 家ã®æºåãã§ããŠãããã©ãããå®æçã«è«è² æ¥è ã«å°ããŸãã
- 家ã®æºåãã§ããããè«è² æ¥è ã«ç¥ãããŠãã ããã
JavaScriptã®èŠ³ç¹ããã3ã€ã®ãªãã·ã§ã³ããããŸãã
- éåææ©èœã®çµæãšããŠã®ã¿å€æŽã§ããã·ã¹ãã ã®ã¹ããŒã¿ã¹ãå®æçã«ç¢ºèªããŠãã ããã
- ã³ãŒã«ããã¯é¢æ°ïŒã³ãŒã«ããã¯ïŒãç»é²ããéåæããã»ã¹ã®çµããã«å¶åŸ¡ã転éããŸãã
- éåæã¢ã¯ã·ã§ã³ã®æåŸã«ãäœããã®çš®é¡ã®ãã³ãã©ãŒããã³ã°ãããããã«ãªãã¹ã³ããã€ãã³ããçºè¡ããŸãã
ãããã®ãªãã·ã§ã³ã詳ããèŠãŠã¿ãŸãããã
ç§ã¯ãæåã®ãªãã·ã§ã³ã¯ã¿ã€ããŒãšå€æ°ã®ãã§ãã¯ã«åºã¥ããŠãããããè¯ããªããšäž»åŒµããŸãã æãåçŽãªå Žåãç¶æ ã¯ããŒã«å€æ°ã§ãã ãã ããéåæé¢æ°ã¯ã2ã€ã®ç¶æ ã ãã§ãªããããã«å€ãã®ç¶æ ãæã€ãªããžã§ã¯ãã®åŠçã«ãªããŸãã ãããŠãå®äºããç¶æ ã®åçµã¿åããã«å¥ã ã«å¯Ÿå¿ããå¿ èŠããããŸãã éåæã¢ã¯ã·ã§ã³ã®çµäºæã«ããŒã«å€æ°ãfalseããtrueã«å€æŽããããšã«ãã£ãŠã®ã¿ã·ã¹ãã ç¶æ ã«åœ±é¿ãäžããããšãã§ãã3ã€ã®éåæåŒã³åºããæ³åããŠãã ããã ãã®ç¶æ³ã§ã¯ãã·ã¹ãã ã®8ïŒ2 3 ïŒã®äžè¬çãªç¶æ ãæ¢ã«çºçããŠããŸãã ãã®ãããªã¢ãŒããã¯ãã£ã¯å®éã«ã¯æ§æäžå¯èœã§ãããè€éãªã¢ããªã±ãŒã·ã§ã³ã§ã¯ã¬ã€ã¢ãŠãã®ã·ã³ãã«ãã決å®çãªèŠçŽ ã§ããããšãããããããŸãã ç¶æ ã®çµã¿åããããã§ãã¯ããããšã¯ãç¹ã«ããžãã¯ã®åœ±é¿ãåããªãå Žåã¯ç°¡åãªããšã§ã¯ãããŸããã æããã«ãã³ãŒãã®ã¯ãªãŒã³ããšæ確ãã®èŠ³ç¹ãããããã¯å®å šãªæªå€¢ã§ãã
ãã®ãããªãã©ã°ã¡ã³ããã³ãŒãå
ã§ãã©ãã·ã¥ãããããªãã§ããïŒ
setTimeout(function(){ if(state1 == 'success' && state2 == 'success'){ ... }else if(state1 == 'success' && state2 == 'error'){ ... }else if(state1 == 'error' && state2 == 'success'){ ... }else if(state1 == 'error' && state2 == 'error'){ ... }else{ setTimeout(arguments.callee, 50); // } },50
ã ããç§ãã¡ã®ãªãã·ã§ã³ã¯äœã§ããïŒ
æåã®ãªãã·ã§ã³ã¯Promiseãã¹ã§ã
ãã¹ãŠãéåžžã«ç°¡åã§ããã³ãŒã«ããã¯é¢æ°ãéåæé¢æ°ã«æž¡ããéåæé¢æ°ã¯æåŸã«åŒã³åºããŸãã
function Build(layout, onComplete){ //... async onComplete(house); } Build(layout, function(buildedHouse){ return house = paintRoof(buildedHouse, 'red'); });
ãã®æ¹æ³ã§ãé ããæ©ãããPromiseAPIã«å°ãããŸããPromiseAPIã¯ãéåæã¢ã¯ã·ã§ã³ã®å®äºã®2ã€ã®è«ççµæïŒæåã®å Žåãšãšã©ãŒã®å ŽåïŒã«å¿çããæ©èœãæäŸããŸãã èªåã§å®è£ ããªãå ŽåããŸãã¯æ¢æã®PromiseAPIå®è£ ïŒ Qãªã©ïŒã䜿çšããå Žåãäžè¬çãªå®è£ ãšåæ§ã«ãéåæé¢æ°ã®ç°ãªãé¢æ°ã«2ã€ã®ã³ãŒã«ããã¯ãæž¡ãããšãã§ããŸãã ããã«ãããå€æŽãåžžã«ç£èŠãããšããåé¡ã解決ã§ããŸãã å€æŽãè¡ãããã®ã§ãã³ãŒã«ããã¯é¢æ°ã¯èªåçã«èµ·åããŸãã
function Build(layout, success, error){ //... ok - true , return ok ? success(house) : error(new Error("- ")); } Build(layout, function(buildedHouse){ return house = paintRoof(buildedHouse, 'red'); }, function(error){ throw(error); } );
ãã®ã¢ãããŒãã«ã¯æãããªæ¬ ç¹ããããŸãã ãŸããéåæã¢ã¯ã·ã§ã³ã®ã·ãŒã±ã³ã¹ã®å Žåã®é¢æ°ã®ãã¹ãããããã«ãããããšãã°ããã§ã«3ã€ã®ã¢ã¯ã·ã§ã³ãããå Žåã次ã®ããã«ãªããŸãã
async1(args, function(response){ async2(response, function(response){ async3(response, function(response){ hooray(response); // Node.js. }); }); });
ãããŠã第äºã«ã管çã®æè»æ§ã®æ¬ åŠïŒã¢ããªã±ãŒã·ã§ã³ã®åéšåãéçºããã³ãŒã«ããã¯ã®æ£ããå®è¡ãä¿èšŒãã責任ãè² ããŸãã åé¡ã¯ä¿èšŒãããããŠããŸããã ã³ãŒã«ããã¯ã®1ã€ã確å®ã«æ©èœããŸãããããPromiseã®ãã€ã³ãã§ã ã åé¡ã¯ãåã³ç«¶äºåã®ããã€ãã³ããäœæãããå Žåãã©ã®ã³ãŒã«ããã¯ãæåã«æ©èœãããããããªãããšã§ãããå Žåã«ãã£ãŠã¯éèŠã§ãã å¥ã®åé¡ïŒäžéšã®ã¢ãžã¥ãŒã«ã®ã³ãŒã«ããã¯ãä»ã®ã¢ãžã¥ãŒã«ã®éåæé¢æ°å ã«ç»é²ããããããæ©èœãããããã³ãŒã«ããã¯ãããã¯ãã¢çµç±ã§ã¢ããªã±ãŒã·ã§ã³ã®å€éšç¶æ ã«æ¥ç¶ããããã°ããŒãã«ïŒãŸãã¯ã¢ãžã¥ãŒã«éã§é£æ¥ïŒããŒã¿ã䜿çšããå¿ èŠããããŸãã äž¡æ¹ã®ãªãã·ã§ã³ã¯ãããšãã°ãæé«ã®ã¢ã€ãã¢ã§ã¯ãããŸããã 競åããã€ãã³ããæ··åšããªãããã«ç¹å¥ã«èšèšãããã¢ãŒããã¯ãã£ãæ éã«èšèšããå¿ èŠããããŸãããããé«ãã¬ãã«ã®æœè±¡åã䜿çšããŠåæ§ã®ãã¹ãŠã®ã±ãŒã¹ã«é©çšã§ããŸãã éåæã¹ããªãŒã ã«äœããã®ã©ãããŒãäœæã§ããããšãããã«ããã£ãå Žåãããã§ãšãããããŸããPromiseAPIã®ã¢ã€ãã¢ãæãã€ããŸããã 幞ããªããšã«ãçŸåšã次ã®ã¹ã¿ã€ã«ã§èšè¿°ããæ©èœããããŸãã
async1.then(async2).then(async3).then(hooray);
æè¯ã®éšåã¯ããã€ãã£ãã³ã³ããŒãã³ããå«ãPromiseAPIã®äœ¿çšãæšå¥šãããã¶ã€ã³ãã¿ãŒã³ããã€ã§ãéžæã§ããããšã§ãã ææ°ã®MV * JavaScriptãã¬ãŒã ã¯ãŒã¯ã¯ããã«åºã¥ããŠããŸãã è¯ãäŸã¯ãAngular.jsã®$ qââãµãŒãã¹ã§ãã
ãã·ã¥ãã£ã€ã¯ãšããŠããããŠããªãããã¥ãŒã¹ãè¿œããªããããªãã¯ããã€ãã®ææ°ã®ãã©ãŠã¶ããã§ã«promiseã®ãã€ãã£ãå®è£ ããµããŒãããŠããããšã«é©ããªãã§ãããã PromiseAPIä»æ§ã®æ€èšã¯ãã®èšäºã®ç¯å²å€ã§ããã ãã®èšäºãèªãã§Common.js Promisesä»æ§ã«ç²Ÿéããããšã匷ããå§ãããŸãã
ãªãã·ã§ã³2-ãã/ãµããã¹
éåæé¢æ°ã¯ãã€ãã³ããçºè¡ããããšã«ããå®äºããããšãå ±åããŸãã ãã®å Žåãã€ãã³ããçºè¡ããã³ãŒãã®éšåãšãã€ãã³ãã«å¿çããã³ãŒãã®éšåãè«ççã«åé¢ããŸãã äœæäžã®ã¢ããªã±ãŒã·ã§ã³ãè«ççã«è€æ°ã®ã¢ãžã¥ãŒã«ã«æ確ã«åå²ã§ããåã¢ãžã¥ãŒã«ãå³å¯ã«å®çŸ©ãããæ©èœãå®è¡ããããçžäºäœçšããå¿ èŠãããå Žåãããã¯æ£åœåãããå¯èœæ§ããããŸãã
äŸ
var manager = {/* */} function Build(layout){ //... , manager.emit({ "type" : "ready", "msg" : " ", "house" : buildedHouse }); } manager.on("ready", function(event){ return house = paintRoof(event.house, 'red'); }); Build(layout);
äžèŠããã®ã¢ãããŒãã¯éåæã¡ãœããã«ã³ãŒã«ããã¯é¢æ°ãç»é²ããããšãšå€§å·®ãããŸããããäž»ãªéãã¯ãã€ãã³ãã®ãããªãã·ã£ãŒãšãµãã¹ã¯ã©ã€ããŒéã®ããåããèªåã§èª¿æŽããããšã§ããããã«ãããããçšåºŠã®èªç±åºŠãåŸãããŸãããã¡ãã£ãšãŒã¿ãŒ "ïŒã ãã®ã¢ãããŒãã®äž»ãªæ¬ ç¹ã¯ãã€ãã³ãã®ãªããžã§ã¯ãããªãã¹ã³ããç»é²ãããã³ãŒã«ããã¯ãåŒãèµ·ããç¹å®ã®ã€ãã³ããµãã¹ã¯ã©ã€ããŒãå¿ èŠãªããšã§ãã ããã¯åå¥ã®ãªããžã§ã¯ãã§ããå¿ èŠã¯ãªãïŒåã®äŸã®ããã«ïŒãå®è£ ã«ã¯å€ãã®ãªãã·ã§ã³ããããŸãã å€ãã®å Žåãã¢ãžã¥ãŒã«éã«ããŸããŸãªçš®é¡ã®ãè«çå±€ãããããŸããããã¯ãä»ã®ã¢ãžã¥ãŒã«ã®ã³ã³ããã¹ãå€ã§ã¢ãžã¥ãŒã«ã®çžäºäœçšãè¡ãæ¬äŒŒã¢ãžã¥ãŒã«ã§ãã ãã ããçŽæãšæ¯èŒããŠããã®ã¢ãããŒãã¯ããæè»ã§ãã
éåæé¢æ°ã¯ããã€ã³ãã£ã³ã°ã¡ãœããã䜿çšããŠãªããžã§ã¯ããè¿ãããšãã§ããŸããããã¯ãPromiseAPIã®å Žåãéåæé¢æ°ãpromiseãªããžã§ã¯ããè¿ãæ¹æ³ãšåæ§ã§ãã
function Build(layout) { ... return { bind : function(event, callback){ // bind } } } var house = Build(layout); house.bind('ready', function(event){...});
å³å¯ã«èšãã°ãããã¯Pub / Subãã¿ãŒã³ãå®è£ ããã ãã®åé¡ã§ãã ã©ã®ããã«ãããè¡ããã¯ããã»ã©éèŠã§ã¯ãããŸããã NodeJSã§æžããå Žåã¯ãEventEmitterã«ç²ŸéããŠããå¿ èŠããããŸãããããŠãã¯ã©ã¹ãã€ãã³ãã®çºè¡ãšãªãã¹ã³ã®ã¡ãœããã䜿çšã§ããããã«ããããšã®éèŠæ§ãç解ããŠãã ããã ãã©ãŠã¶ã®ããã°ã©ãã³ã°ã«é¢ããŠã¯ãããªãã®æ°ã®ãªãã·ã§ã³ããããŸãã ã»ãšãã©ã®å Žåãé ããæ©ããã䜿çšãããã¬ãŒã ã¯ãŒã¯ã®ããªã¬ãŒã¡ãœããã䜿çšããããšã«ãªããŸããã»ãšãã©ã®MV *ãã¬ãŒã ã¯ãŒã¯ã§ã¯ããããç°¡åãã€ç°¡åã«è¡ãããšãã§ããŸãïŒããã³äžéšã®:)ããããŸã£ããè¡ããªãããšãã§ããŸãïŒã ãããã«ãããçè«ã¯ååã«è©³çŽ°ã«èª¬æãããŠããŸãã è¯å®çãªäŸã®1ã€ã¯ãã¢ãžã¥ãŒã«ãã¡ãµãŒãã¡ãã£ãšãŒã¿ãŒãã¿ãŒã³ã®çµã¿åããã§ã ãããã«ã€ããŠã¯ã ãã¡ããåç §ããŠãã ãã ã
èšèš
å€ããå°ãªãã倧èŠæš¡ãªã¢ããªã±ãŒã·ã§ã³ã®äœæãéå§ãããšãã¯ãã¢ãŒããã¯ãã£ã®è«çéšåãåé¢ããŠãäºãã«å¥ã ã«éçºããã³ä¿å®ã§ããããã«ããå¿ èŠããããŸãã éåæããã°ã©ãã³ã°ã§ã¯ãã¢ãžã¥ãŒã«ã¯APIã¡ãœãããåŒã³åºããŠããã«çµæãè¿ããªããããã¢ãžã¥ãŒã«ãžã®èŠæ±ã®é 次å®è¡ãšã¢ããªã±ãŒã·ã§ã³ã®ä»ã®éšåã«ããå¿çã®åŠçã¯åºæ¬çã«äžå¯èœã§ãã å€éšããã¢ãžã¥ãŒã«å ã«ãã³ãã©ãŒãç»é²ããæ©èœã¯éåžžã«æºè¶³ã®ããæ¹æ³ã§ãããã¢ãžã¥ãŒã«éã®çžäºäœçšãæ¡åŒµããäºå®ãããå Žåã¯ããã³ãŒã«ãã«ã«ãã«é¥ãå¯èœæ§ãããããšãç解ããå¿ èŠããããŸãã ãã®äžæ¹ã§ã æçµçãªAPIãæäŸããéåžžã«åçŽãªã¢ãžã¥ãŒã«ããããŸãããããã¯æ¡åŒµãé£ãããããã³ãŒã«ããã¯ã®çŽæ¥å®è£ ã®ã¢ãŒããã¯ãã£ãèŠä»¶ã«é©åããå ŽåããããŸãã
ããšãã°ãajaxããªããŒããŒã管çããjQueryããŒã¹ã®ã¢ãžã¥ãŒã«
PromiseApiåŽã«åãæ¿ããããšã«ããå Žåããããã®ãã¹ããåé€ããå°ããªå€æŽãå ããŠæ¬¡ã®ããã«èšè¿°ããŸãã
éåžžã«å®£èšçã§ãã ãããŠãAjaxPreloaderã¢ãžã¥ãŒã«ãåŒæ°ãšããŠå¥ã®ã³ãŒã«ããã¯ãå¿ èŠãšããé¢æ°ãè¿ãããšã¯æ±ºããŠãªãããšãç¥ã£ãŠãç§ãã¡ã¯å¹³åã«ç ãããšãã§ããŸãã ãã®ãããªã¢ãžã¥ãŒã«ã ããèšèšã§ããå Žåã¯ããããè¡ããŸãã ã¢ãžã¥ãŒã«ãç¹ã«å ¬éAPIãåçŽã§ããã°ããã»ã©è¯ãã§ãã
var AjaxPreloader = (function(){ function AjaxPreloader(spinner){ this.spinner = spinner; } AjaxPreloader.prototype.show = function(onComplete) { this.spinner.fadeIn(onComplete); return this; }; AjaxPreloader.prototype.hide = function(onComplete) { this.spinner.fadeOut(onComplete); return this; }; return AjaxPreloader; })(); var preloader = new AjaxPreloader($("#preloader")); preloader.show(function(){ div.load("/", preloader.hide); });
PromiseApiåŽã«åãæ¿ããããšã«ããå Žåããããã®ãã¹ããåé€ããå°ããªå€æŽãå ããŠæ¬¡ã®ããã«èšè¿°ããŸãã
preloader .show() .then(function(){ return div.load('/') }) .then( function(response){}, //success function(error){} //error ) .always(preloader.hide);
éåžžã«å®£èšçã§ãã ãããŠãAjaxPreloaderã¢ãžã¥ãŒã«ãåŒæ°ãšããŠå¥ã®ã³ãŒã«ããã¯ãå¿ èŠãšããé¢æ°ãè¿ãããšã¯æ±ºããŠãªãããšãç¥ã£ãŠãç§ãã¡ã¯å¹³åã«ç ãããšãã§ããŸãã ãã®ãããªã¢ãžã¥ãŒã«ã ããèšèšã§ããå Žåã¯ããããè¡ããŸãã ã¢ãžã¥ãŒã«ãç¹ã«å ¬éAPIãåçŽã§ããã°ããã»ã©è¯ãã§ãã
äžéšã®ããŒã«ãéžæããã¿ã€ãã³ã°ãšãä»ã®ããŒã«ãéžæããã¿ã€ãã³ã°ãç解ã§ããããšãéèŠã§ãã
確ãã«å°ããªã¢ããªã±ãŒã·ã§ã³ãäœæãã次ã®ã¹ããŒã ã䜿çšããå¿ èŠããããŸããã
var root = $("#container"); // , root.on("someEvent", function(){ // }); root.trigger("someEvent"); // root
äžéšã®ã¢ããªã±ãŒã·ã§ã³ã¢ãžã¥ãŒã«å ã«ã³ãŒã«ããã¯ãç»é²ãããç¹ã«å®è¡ã³ã³ããã¹ãã«æ³šæãæãããã¢ããªã±ãŒã·ã§ã³ããŒãã®è«ççãªåé¢ãç¶æããããã«ãå€ãã®ãŠãŒã¶ãŒã¯åã«DOMèŠçŽ ã§ã«ã¹ã¿ã ã€ãã³ããçºè¡ãããããä»ã®å Žæã§ãã£ããããŸãã¢ããªã±ãŒã·ã§ã³ã®å Žæãšå¿ èŠãªã¢ã¯ã·ã§ã³ãå®è¡ããŸãã ãããã£ãŠãã¢ãžã¥ãŒã«ã¯1ã€ã®èŠçŽ ã®ã¿ã«äŸåããŠãããã³ãŒã«ããã¯ãç»é²ãã代ããã«ãè¿œå ã®ãã©ã¡ãŒã¿ãŒãã¢ãžã¥ãŒã«ã«æž¡ãã ãã§ããããã¯ããªãã¹ã³ããããã¥ã¡ã³ãèŠçŽ ã§ãã å³å¯ã«èšãã°ãããã¯ããªãè°è«ã®äœå°ã®ãããã©ã¯ãã£ã¹ã§ãããã¢ã€ãã¢èªäœã¯è¯ããã®ã§ãã ã¢ãžã¥ãŒã«ãã€ãã³ããçºè¡ããã¢ããªã±ãŒã·ã§ã³ãã€ãã³ãããªãã¹ã³ãããšãå€ãã®ç¹ã§éåžžã«äŸ¿å©ã§ãã
æšæºã®Pub / Subã¡ãœããã§ã¢ãžã¥ãŒã«ãæ¡åŒµãããªããžã§ã¯ãã®ã©ãããŒã䜿çšããããšã¯ç§ã«ãšã£ãŠäžè¬çã«ãªã£ãŠããŸãã
var module = PubSub({ load: function(url){ ... this.emit('loaded', data); } }); module.on('loaded', function(data){ ... });
ãã®å Žåãã¢ãžã¥ãŒã«ã¯ã€ãã³ããçºè¡ããããèªäœããµãã¹ã¯ã©ã€ããŒã§ãã 代æ¿ã¢ãŒããã¯ãã£-ãã¹ãŠã®ã¢ãžã¥ãŒã«ã®1ã€ã®ãµããµãã»ã¯ã·ã§ã³ã¯å šäœãšããŠããéäžåãããŠããããã«èŠããŸãããããã¯ã¢ãžã¥ãŒã«ã€ãã³ããšã¢ããªã±ãŒã·ã§ã³ã€ãã³ãã®éã®åãªãå¥ã®ã¬ã€ã€ãŒã§ãã ã»ãšãã©ã®å Žåãããã¯å¿ èŠã§ã¯ãããŸãããããã«ãã€ãã³ãã®åéãšãã³ãã©ãŒã®ç»é²ã®ã¿ã«é¢äžãããããäœãã«ãã£ãŠåŒãèµ·ããããªãå ŽåïŒããšãã°ãã¢ããªã±ãŒã·ã§ã³ã¢ãŒããã¯ãã£ã®ãã«ãã¹ããŒãžãã¹ãïŒããã®ãããªãªã¬ãŒã¯æ¬è³ªçã«ãã¡ãµãŒãã§ãããšèããŠã¯ãããŸãããäžéå±€ã ããã«ãéäžåã¢ãŒããã¯ãã£ã«å¯Ÿãããã1ã€ã®èšèã¯ãã¢ããªã±ãŒã·ã§ã³ã¢ãžã¥ãŒã«ã®æ°ãå¢ããã«ã€ããŠããã®ããã«ç«¶äºåã®ããã€ãã³ããæ§æããããšããŸããŸãé£ãããªããšããããšã§ãã ã¢ããªã±ãŒã·ã§ã³ã«ããµãŒããŒãšã¯ã©ã€ã¢ã³ãéã§ã®ããŒã¿ã®åæãå«ãŸããã¢ãžã¥ãŒã«ãå«ãŸããè€æ°ã®ã¯ã©ã€ã¢ã³ããååšããå¯èœæ§ããããåå è ãå ±åã®ç«¶åã€ãã³ããçºè¡ããå ŽåããŸããªç¶æ³ãã·ãã¥ã¬ãŒãããŸããã ãµãã¹ã¯ãªãã·ã§ã³ãšãããªã±ãŒã·ã§ã³ã®ãã¹ãä»ããŠã®ã¿ã¢ãžã¥ãŒã«ããªã³ã¯ã§ããå Žåãç°çš®ã€ãã³ãã®ã¬ã€ã¢ãŠããã©ã®ããã«åçŽåããããããç解ããã ããã°å¹žãã§ãã ããã¯ãéäžå¶åŸ¡ãŠããããåŒã£åŒµãå¿ èŠãªãçžäºã«ããåãã§ããã³ã³ããŒãã³ãã«é¢ããŠéåžžã«äŸ¿å©ã§ãã
ã€ãã³ãé§ååã¢ããªã±ãŒã·ã§ã³
æè¿ãç§ã«é¢é£ãããããã¯ã¯ã€ãã³ãã®ã¬ã€ã¢ãŠãã§ãã äžå¿«ãªç¹ã¯ãã€ãã³ããç°ãªãå Žæã§çºçããã ãã§ãªããç°ãªãæéã«çºçããããšã§ãã ç¹å¥ãªææ³ã䜿çšããã«ç°çš®ã®ã€ãã³ããçµã¿åãããã®ã¯ãæ§ããã«èšã£ãŠãäžå¿«ã§ãã ãã®ãã¹ãŠã®ããã«ããéåžžã«æ³¢ä¹±ã«å¯ãã ããšèª¬æã§ããç¹å¥ãªçš®é¡ã®ã¢ããªã±ãŒã·ã§ã³ããããå€ãã®ç°ãªãéšåã§æ§æãããŠããŸãã ãããã®éšåã¯ãçžäºã«ããåããããããŠãŒã¶ãŒãšããåããããããµãŒããŒã«ããŒã¿ãéä¿¡ããããåã«ä¿çç¶æ ã«ãããã§ããŸãã / then / elseãçµã¿åããè³ççºã§ããå ŽåãåŸæ¥ã®åœä»€ã䜿çšããŠãå¯èœãªãã¹ãŠã®ã€ãã³ãã®çµã¿åãããæŽçããããšããŸãã å¥åŠãªããšã«ããã®çš®ã®ã¢ããªã±ãŒã·ã§ã³ã«é©çšãããé¢æ°åããã°ã©ãã³ã°æ¹æ³è«ã¯ã人çããã£ãšæ¥œã«ããŸãã ããŸããŸãªã€ãã³ãéã®è€éãªäŸåé¢ä¿ãã䜿ãæ £ããé¢æ°åããã°ã©ãã³ã°ã®å®£èšã¹ã¿ã€ã«ã§èšè¿°ããæ©èœãæäŸããã©ã€ãã©ãªãããã€ããããŸãïŒBacon.JSãReactive Extensions-RxJSãåç §ïŒã ãã®èšäºã§ã¯ãããã®ã©ã€ãã©ãªãåæããŸãããçŸåšãBacon.jsã«äŒŒãèªå·±èšè¿°ã©ã€ãã©ãªã䜿çšããŠãããšã ãèšããŸãããéåæã¹ããªãŒã ã®ã¬ã€ã¢ãŠããšç Žå£ã«éç¹ã眮ããŠããŸãã ã³ã¡ã³ãä»ãã®äœæ¥ã³ãŒãã®ãã©ã°ã¡ã³ããæäŸããŸãã
Webãœã±ããã䜿çšããããç©å
·ã®Swarmationã®ã³ãŒãã¹ãããã
// var keyups = obj.stream('keyup'), arrowUps = keyups.filter(isArrows); // function isArrows(which){ ... } // function vectorDirection(which){ ... } // event.which vector2 function allowedMoves(direction){ ... } // function isWinerPosition(pos){ ... } // // enemy.moves = socket.stream('playerMoves'); // player.moves = arrowUps.filter(allowedMoves).map(vectorDirection); // game.ticks = timer.stream('tick'); // game.pause = keyups.filter(function(which){ return which==19}); // // game.ticks.syncWith(enemy.moves).listen(redraw); // player.moves.listen(redraw); // game.ticks .syncWith(enemy.moves) // .produceWith(playerMoves, function(pos1, pos2){ // if(cmp.equals(pos1, pos2)){ // socket.emit('win'); // game.ticks.lock(); // game.emit('loose', { position : pos1, }); } }); game.pause.toggle([ // function(){ game.ticks.lock(); // player.moves.lock(); // socket.emit('pause'); // - }, function(){ game.ticks.unlock(); // player.moves.unlock(); socket.emit('run'); } ]); player.moves .filter(isWinnerPosition) // , .listen(function(pos){ game.ticks.lock(); // socket.emit('loose'); // game.emit('win', { // . ! position: pos }); });
, , ( . , : ? -.
, !