开发者

How to move an anonymous function out

I have a $.ajax call with an error callback:

error: function(result){
$('#msg').text(result.statusText).add开发者_运维知识库Class('err');
}

I'd like to change it to a more generic:

error: myError(result)

And then all by itself:

function myError(theError){
$('#msg').text(theError.statusText).addClass('err');
} 

But firebug is telling me "result" is not defined.

Q: Do I call it this way?

error: function(result){
myError(result);
}


You just want the function name, not any parenthesis or arguments, like this:

error: myError

It'll call that function reference with the same arguments as it would an anonymous function, right now it's trying to actually execute the function (with result as an argument...a variable it can't find) and assign the result as an error handler, you want the function itself to actually run as the error handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜