<script> (function(v){alert(v)}("aa")); hoge=function(v){alert(v)}; </script>
ã£ã¦ãã£ãå ´åããã¼ãã£ã³ã°ãçµãã£ãã alert("aa") ãå®è¡ãããã
ãã ãã
<script> (function(v){alert(v)}("aa")); hoge=function(v){alert(v)}; hoge("bb"); </script>
ã£ã¦ãã£ãå ´åããã¼ãã£ã³ã°ãçµãã£ãã alert("aa"); alert("bb") ãå®è¡ãããã
functionå
¨ä½ã()ã§ããã£ãå ´åã¯ãããèªä½ãç¡åé¢æ°åãã¦1åå®è¡ãããã
ãããã(function(){})ã¯ä½ãè¿ããï¼
<script> hoge=(function(v){alert(v)})("aa"); alert(hoge); </script>
ã£ã¦ããã¨ãundefinedã«ãªãã
ããã¨ããã®(function(){})ã¯ä½¿ãæ¨ã¦ãã»ã»ã»ã£ã¦ããã¨ãããã§ã¯ãªãã
ã§ããªãã§ããã調ã¹ã¦ããã£ã¦Young risk taker.: [Javascript] クロージャを利用したイベントリスナの登録ãèªãã§ã¦ã
<ul id="selection"> <li> </li> <li> </li> <li> </li> <li> </li> </ul> <script> document.observe('dom:loaded', function() { $('selection').observe('click', (function(){ var SELECTED_CLASS_NAME = 'selected'; var selected; return function(event) { if(selected) selected.removeClassName(SELECTED_CLASS_NAME); event.target.addClassName(SELECTED_CLASS_NAME); selected = event.target; }; })()); }); </script>
ãç解ã§ããªãã£ãããã
ä¸è¨ã®ãScriptã«ã¯3ã¤functionãåºã¦ããã
ãã§ãonclickã«çµã³ä»ãããã¦ããã®ã¯ã1ã¤ã ããäºã¤ç®ã®functionã
ããã()ã§ããããã¦ããã®ã§ãDOMããã¼ãã£ã³ã°ãããã¨ãã«ä¸åº¦èµ°ãã
ã¯ãã¼ã¸ã£ã£ã¦åãã£ã¦ãããã§åãã£ã¦ãªãã£ããã ãã©ããããã£ã¦()ã§ãããããã¨å®å ¨ã«ç¬ç«ãã¡ãã£ã¦ãã®å é¨ãè¦ããã¨ã£ã¦é£ãããã ãé è½ãã¦ãããã ãããã©ã
ããããããã()ã§ããããªãã¦ãã ã®functionã«ãããã©ããªããã
ãã®è¾ºã¦ãããããdocument.observeã®ç¬¬äºå¼æ°ãä½ãããã£ã¦ããã®ãç¥ããªãã¨ãã¡ãªæããã ãã©ããã¦ãã®ã¾ã¾å®é¨ãã¤ã¾ããããªæãã
document.observe('dom:loaded', function() { $('selection').observe('click', function(){ var SELECTED_CLASS_NAME = 'selected'; var selected; return function(event) { if(selected) selected.removeClassName(SELECTED_CLASS_NAME); event.target.addClassName(SELECTED_CLASS_NAME); selected = event.target; }; }); });
ããããã¨ã<ul id="selection">ãã¯ãªãã¯ãããã³ã«2ã¤ç®ã®functionãå®è¡ãããããã£ãã®å ´åã¯å®è¡ã¯ãã¼ãã£ã³ã°ã«1度ã ãã
ããã«ãæå¾
éãã«åããªãã
ãªãã§ãï¼
return function(){.........} ã«ãªã£ã¦ããçç±ã¯ï¼
ããããã3ã¤ç®ã®functionããªãã§return functionã«ãªã£ã¦ãã®ããè¯ãåãã£ã¦ããªãã
ããã¾ã§ãreturn functionã®è¨è¿°ã£ã¦Eventãªãã¶ã¼ãã§åºã¦ãããã¨ããè¦ããã¨ãç¡ãã»ã»ã»ãããããEventãªãã¶ã¼ãã£ã¦ä½ãªãã ã£ã¦è©±ã«ãªãã
ä¸çªç°¡åãªä¾ã¯
<div class="title" id="Etest"> Event Observe test. </div> <script> $('Etest').observe('click', function(event){alert(event.target.id)}); </script>
ãããªãããªã$('Etest')ã¯ã¯ãªãã¯ããããã³ã«ãfunction(){.....}ãå®è¡ããã¦alertãè¿ãã
ãããããã®funciton(){.....}ã()ã§ããã£ã¦ã¿ã
<div class="title" id="Etest"> Event Observe test. </div> <script> $('Etest').observe('click', (function(event){alert(event)})("aaaaaa")); </script>
ããã ã¨ããã¼ãã£ã³ã°ãããã ç¬éã«alert("aaaaaa")ãå®è¡ããã¦çµããã
ãã®å¾$('Etest')ãã¯ãªãã¯ãã¦ããã¨ã©ã¼ãFireBugã«ã¯
handler has no properties
wrapper(click clientX=0, clientY=0)prototype.1.6.0.j... (line 3842)
[Break on this error] handler.call(element, event)
ã£ã¦ã§ã¦ãã
ããã§ãreturn functionãã
<div class="title" id="Etest"> Event Observe test. </div> <script> $('Etest').observe('click', (function(argv){ var step = argv; var num = 0; return function(event){ num = num + step; alert(event.target.id+num); } })(100)); </script>
ããã ã¨ã$('Etest')ãã¯ãªãã¯ãããã³ã«alertãè¿ããããããç¹çãã¹ãã¯ãã¼ã«ã«ã®å¤æ°ã使ããã使ããã¨ããã®ã¯ãæ®ããã£ã¦è¨ãæå³ã§ããªãããå ã¾ãã¦ãã£ã½ããæããããï¼ï¼
ã¨ãããã¨ã§ãã¾ã¨ãã¦ã¿ããããã¯ããªãã§ï¼ãã£ã¦ã¬ãã«ãããªãã¦ããããªã£ã¦ããããã§è¦ãã¦ãããã¨ã«ãã
onclickãonmouseoverã«ãããã¤ãããã¦ããã®ããæ®éã®functionã®å ´å
â ã¤ãã³ããèµ·ãããã³ã«ãã¤ãã³ããã³ãã©ã¯ãã®functionãå®è¡ããã
no_closuer posted by (C)ITOH Takashi
ãã®å ´åããã®functionå ã®ãã¼ã«ã«å¤æ°ãä¿åãã¦ããæ¹æ³ãç¡ãï¼ãã¶ãï¼ï¼ãç°¡åãªä¾ã§è¨ãã¨ã
<div class="title" id="Etest"> Event Observe test. </div> <script> $('Etest').observe('click', function(event){var a=0;a++; alert("hoge"+a);}); </script>
å½ç¶ã ãã©ããã¼ã«ã«å¤æ°aã¯ããã¤ã0ã«åæåãããã®ã§æå³ãç¡ãã
ãªã®ã§ããã£ãæªããã©ã°ãã¼ãã«å¤æ°ã«æ¯ã£ã¦ãããããªããããã£ã¦çµæ§æ°æã¡æªãã
ããã解決ããã®ãã¯ãã¼ã¸ã£ã
onclickãonmouseoverã«ãããã¤ãããã¦ããã®ããã¯ãã¼ã¸ã£ã®å ´å
â ã¤ãã³ããèµ·ãããã³ã«ãã¤ãã³ããã³ãã©ã¯ã¯ãã¼ã¸ã£ãreturnããfunctionãå®è¡ããããã®æãã¯ãã¼ã¸ã£èªä½ã¾ã§ä¿åãã¦ããã¦å®è¡ãã¦ãããã
closuer posted by (C)ITOH Takashi
ããªãã§ãããããï¼ï¼ï¼ï¼ï¼ï¼ãã£ã¦æã£ã¡ãããã©ããããããã ã¨ãããããä»æ§ãªãã ã¨ç解ãããããªãããããã¯ã
ãã ãããªã便å©ã
ããã¦ããã®æåã«æ¸ãã¦ãããããã¼ãæã«ä¸åå®è¡ãã£ã¦ããã®ã¯ãã¯ãã¼ã¸ã£ãä½ãåºãã®ã«å¿ è¦ãªããã»ã¹ãªãã ã¨æã£ããã§ãã¯ãã¼ã¸ã£ã®ã»ããã¢ãããããæã«å¿ è¦ãªã®ã(function(){...........})(ããã§ä¸ããããå¼æ°)ãªãã ãªããã¼ããããããã¨ã
追è¨ï¼
(function(){})() と function(){}() - IT戦記
æã¨å¼ã£ã¦ä½ãéããã ããï¼
ã¦ããâã§ç¥ã£ãã®ã ãã©
function() {} ()
ã¨
function() {}
ã¯ã大ããéãæ°ããããã©ã
(function() {})
ã¨
function() {}
ã¯ã»ã¨ãã©å¤ããæ°ãããªãã
ã±ãã®æ¬å¼§ã®æ¹ãéè¦ãªã®ããªãï¼
追è¨ï¼ï¼
http://d.hatena.ne.jp/teramako/20080208/p1
ã§åãã£ããã ãã©ã
ããããã±ãã«()ãä»ããã¨ç¡åé¢æ°ãä½ã£ã¦ããå®è¡ã£ã¦ãã¨ã«ãªãã®ãããã¼ãããããããããããè¨ããã¦ã¿ãã°ããã ãã©æ°ãã¤ããªãã£ããã»ã»ã»ã»ã»ãorzã
é¢æ°ãªãã¸ã§ã¯ãã«()ã§ããå®è¡ããããããããã
ã§ããreturnã§functionãè¿ãã®ãã¯ãã¼ã¸ã£ã®ããæ¹ã£ã¦ãããããããã ã£ã¦ã®ã¯ééããããªãã®ãã»ã»ã»ãªï¼