JS 技巧

 1.js 绑定事件共用

// 函数就是像strings或numbers那样的数据,我们可以把它们当成函数参数来传递它们,这可以创建十分令人惊讶而又“威风凛凛”的Web应用程序。这个方法是非常有用的,几乎所有的主流框架都使用了这个方法。
function byId(element, event, f){ 
   Document.getElementById(element).['on'+event] = f; 
   //if is the function that we pass as parameter 
} 

// 调用单击事件
byId('myBtn','click',function(){alert('Hello World')}); 

2.把函数当做对象来定义,可传递参数操作

// 把函数当做对象来定义,可传递参数操作
var msg = function(m){ alert(m);} 

// 调用
msg('Hello world'); //This will work 

 

posted @ 2015-09-23 07:03  Duke-码动青春  阅读(394)  评论(0)    收藏  举报