Deferred 对象文档说它可以采用可选的函数参数,但没有示例。我该如何使用它?
问问题
194 次
2 回答
1
它是一种语法糖,通过它您可以在相同的延迟对象上添加解析或拒绝的回调/处理程序。
所以,下面的两个代码片段会做同样的事情。
// with constructor method argument
var x = $.Deferred(function(selfDfd){
selfDfd.done( console.log.bind(console,'I am resolved,in constructor'));
});
x.resolve();
// without constructor method argument
var y = $.Deferred();
y.done(console.log.bind(console,'I am resolved, NO constructor args'))
y.resolve();
于 2012-12-04T00:07:30.363 回答
0
使用以下语法:
function foo(){ console.log("Yes!!!")}
function bar(){ console.log("Are we there yet?")}
function baz(){ console.log("I can't hold it much longer!?")}
with($.Deferred() )
{
done(foo);
bar();
baz();
resolve(foo);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
生成器将具有相同的效果:
var foobarbaz = function(i){var count = count || i; return function(){console.log("Yes!!!"); return ++count;}}
var bar = function(){ console.log("Are we there yet?")}
var baz = function(){ console.log("I can't hold it much longer!?")}
var done = foobarbaz(0);
bar();
baz();
done();
参考
于 2012-12-02T01:22:54.353 回答