在绑定事件处理程序时,我发现需要创建一个函数,因为它需要被引用两次;最初一次,一次到事件绑定:
// Define function
function doSomething(){...}
// Call function initially
doSomething();
// Pass function as the event handler
$("#theElement").on("click", doSomething);
但后来我意识到我可以通过传递一个自调用匿名函数作为事件处理程序并返回来开始这样做arguments.callee
:
// Much cleaner!
$("#theElement").on("click", (function(){
...
return arguments.callee;
})());
假设我从未在这两个实例之外使用此功能,这样做是否可行?