method chain
(function(){
function chain() {
}
chain.prototype = {
alert: function(value) {
alert(value);
return this;
}
};
new chain().alert(1).alert(2).alert(3);
})()
function chain
(function(){
function chain(value) {
alert(value);
return chain;
}
chain(1)(2)(3);
})()
arbitrary function
(function(){
function chain(f) {
return function chain_impl() {
f.apply(null, arguments);
return chain_impl;
}
}
chain(alert)(1)(2)(3);
})()
append method
(function(){
function chain(f) {
function chain_impl() {
f.apply(null, arguments);
return chain_impl;
}
chain_impl.hoge = function(value) {
alert(value);
return chain_impl;
};
return chain_impl;
}
chain(alert)(1)(2).hoge('fuga')(3);
})()
function chain 2
(function() {
function chain(f) {
f();
return chain;
}
chain(function(){alert('a')})
(function(){alert('b')})
(function(){alert('c')})
})()
(function() {
function chain(init) {
var result = init;
return function next(f) {
result = f(result);
return next;
};
}
chain('a')
(function(v){alert(v); return 'b'})
(function(v){alert(v); return 'c'})
(function(v){alert(v)})
})()
setTimeout
(function() {
function chain(f) {
var funcs = [Function(), f];
function fire() {
funcs.shift()();
if (funcs.length > 0) {
setTimeout(fire, 500);
}
}
fire();
return function add(f) {
funcs.push(f);
return add;
};
}
chain(function(){alert('a')})
(function(){alert('b')})
(function(){alert('c')})
})()
(function() {
function next(f) {
function D() {
function instance(f) {
return instance.next(f);
}
instance._next = null;
instance.callback = function (x) { return x; };
instance.next = function(f) {
this._next = new D();
this._next.callback = f;
return this._next;
};
instance.call = function(value) {
value = this.callback.call(this, value);
if (value && typeof value._next != 'undefined') {
value._next = this._next;
} else {
if (this._next) this._next.call(value);
}
};
instance.wait = function(n) {
return this.next(function() {
var d = new D();
setTimeout(function () {
d.call();
}, n * 1000);
return d;
});
};
if ('__proto__' in instance) instance.__proto__ = this;
return instance;
}
var d = new D();
d.callback = f;
setTimeout(function() { d.call(); }, 0);
return d;
}
next(function(){alert('a')}).wait(0.5)
(function(){alert('b')}).wait(0.5)
(function(){alert('c')})
(function(){alert('d')})
})()