如果我这样做:
$('h1').slideUp('slow', function() { $('div:first').fadeOut(); });
h1 会向上滑动,然后第一个 div 会淡出。
但是,如果我这样做:
function last() { $('div:first').fadeOut(); }
$('h1').slideUp('slow', last());
h1会向上滑动,div会同时淡出!
如何使我的第二个示例与第一个示例一样工作,其中 fadeOut() 在 slideUp() 之后被调用?
最佳答案
你不需要使用函数返回值(通过调用函数得到),但是函数体:
$('h1').slideUp('slow', last);
你所做的与此相同:
var returned = last(); // call to last returns undefined
// so returned has the value undefined
$('h1').slideUp('slow', returned); // simply sending undefined as a callback
所以你只是内联执行了 last
函数,然后将返回值(它是 undefined
因为它什么都不返回)作为参数传递给 slideUp
的回调函数。
希望这个例子能帮助你理解:
function outer() {
function inner() {};
return inner;
}
alert(outer); // returns the outer function body
alert(outer()); // returns the outer function's return value, which is the inner function
关于javascript - jQuery:将匿名回调重写为命名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28268338/