Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Created February 27, 2011 01:11
Show Gist options
  • Save mkuklis/845803 to your computer and use it in GitHub Desktop.
Save mkuklis/845803 to your computer and use it in GitHub Desktop.
chain
function chain() {
var cur = prev = null;
for (var i = l = arguments.length - 1; i >= 0; i--) {
prev = cur;
cur = (function(params) {
return function() {
for (var param in params) {
this[param] = params[param];
}
}
})(arguments[i]);
if (i < l) {
cur.prototype = new prev;
}
}
return new cur;
}
var a = {a: 1}, b = {b: 2}, c = {c: 3},
chained = chain(a, b, c);
console.log(chained.c); // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment