我想知道如何實現這種方法鏈風格啓用鏈接方法:使用「.delay」
var foo = function(){};
foo.prototype = {
say : function(s){
alert(s);
return this;
},
delay : function(ms){
var _this = this;
setTimeout(function(){
return _this;
}, ms);
return this; // If I omit this, there will be an error.
}
}
var bar = new foo();
bar.delay(2000).say('hello'); // this will immediately alert 'hello'.
我想用這樣的:
bar.delay(2000).say('hello'); // in 2 sec. say 'hello'.
不喜歡這個:
bar.delay(2000, function()...); // it's not a chained method
可能嗎?
謝謝!我看到這個概念。所以似乎我需要使用類似隊列的「包裝」功能對我的原始代碼進行大的修改。 [隊列實現]鏈接看起來非常有趣。我會稍後閱讀。 – martin 2010-08-06 14:42:49