Last active
April 8, 2016 20:01
-
-
Save eduardonunesp/18a455de1fe2f26d3483 to your computer and use it in GitHub Desktop.
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.prototype.partial = function() { | |
| // CODE HERE | |
| var fn = this, args = Array.prototype.slice.call(arguments); | |
| return function() { | |
| var replace = function(arr) { | |
| var pos = 0; | |
| return { | |
| next : function() { | |
| return arr[pos++]; | |
| } | |
| }; | |
| }; | |
| var replacer = replace(Array.prototype.slice.call(arguments)); | |
| var t = []; | |
| args.forEach(function(arg) { | |
| if (arg) { | |
| t.push(arg); | |
| } else { | |
| t.push(replacer.next()); | |
| } | |
| }); | |
| return fn.apply(this, t); | |
| }; | |
| }; | |
| var hi = function(a, b, c, d) { | |
| console.log(a + ' ' + b + ' ' + c + ' ' + d); | |
| }; | |
| var f = hi.partial(undefined, '2', undefined, '4'); | |
| f('1', '3'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bacana ! my 2cents !