定义一个函数马上执行 与 循环内共享内部变量

星期五 天气:晴
 
今天起的早 溜狗去了 神清气爽
 
现在脚本被大规模应用了 呵呵 2.0 能撑到什么时候
 

<div id="testDiv" style="background-color:red;height:100;width:100"/>
<SCRIPT LANGUAGE="JavaScript">
<!--
var div = document.getElementById("testDiv");
var events = {onclick: "clicked", onchange: "changed",  onmouseover: "mouse over"};

/*
for(e in events){
   div[e] = function(e){
       return function(){
           alert(events[e]);
       };
   }(e);
}
*/

for(e in events){
 
 var f = new Function("arg", "return function(){alert(events[arg])}");
 div[e] = f(e);
 
}

(function(e){
 alert(e);
})(0);

//-->
</SCRIPT>

Advertisements
此条目发表在JavaScript分类目录。将固定链接加入收藏夹。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google+ photo

You are commenting using your Google+ account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s