Created
April 12, 2010 02:19
-
-
Save NV/363208 to your computer and use it in GitHub Desktop.
JavaScript chaining
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(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