Created
April 30, 2015 08:14
-
-
Save Semigradsky/28ff249b5886819d6693 to your computer and use it in GitHub Desktop.
part apply
This file contains 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
// функция, над которой будем издеваться | |
var add = function() { | |
return [].reduce.call(arguments, function(a,b) { return a+b; }, 0); | |
}; | |
var part = function(fn, a) { | |
return function() { | |
var args = a.slice(), j = 0; | |
for (var i = 0; i < args.length; i++) { | |
if (args[i] === part.none) { | |
args[i] = arguments[j++]; | |
} | |
} | |
// копируем оставшиеся параметры | |
if (j !== args.length) { | |
args = args.concat([].slice.call(arguments, j)); | |
} | |
return fn.apply(null, args); | |
} | |
}; | |
// какое-то значение, которым будем помечать то, что на этой позиции нет параметра | |
part.none = '***bla-bla-9000***'; | |
var partAdd = part(add, [1, part.none, 2]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment