假设有一个功能:
function doSomething(){
attachEvent("click", function(){
//there is a lot of code in this anonymous function
//It may need to be defined somewhere else as a named function
doSomethingElse = variable;
});
}
如何在其他地方定义它并传入一个变量
function doSomething(){
//this fires straight away
attachEvent("click", doNamedFunction(this.variable));
}
function doSomething(){
//works but arguments aren't being passed
attachEvent("click", doNamedFunction);
}
//I am defined as a named function instead of an anonymous function
function doNamedFunction(arg){
doSomethingElse = arg;
//do lots of other stuff below
}
所以问题是如何声明命名函数并传递参数来代替匿名函数,还是你总是需要使用匿名函数作为回调..?
提前谢谢了!