Skip to content

Instantly share code, notes, and snippets.

@NV
Created April 12, 2010 02:19
Show Gist options
  • Select an option

  • Save NV/363208 to your computer and use it in GitHub Desktop.

Select an option

Save NV/363208 to your computer and use it in GitHub Desktop.
JavaScript chaining
function chain(fn){
// Chain factory
var list = [fn];
return function oneChain(fn){
if (arguments.length) {
list.push(fn);
return oneChain;
} else {
var result;
while (list.length) {
result = list.shift()(result);
}
return result;
}
};
}
function three(){return 3}
function square(n){return n*n}
chain(three)(square)() === square(three());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment