Created
February 27, 2011 01:11
-
-
Save mkuklis/845803 to your computer and use it in GitHub Desktop.
chain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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