Created
March 27, 2015 18:59
-
-
Save gilbert/be35843f7cabb7235d54 to your computer and use it in GitHub Desktop.
JavaScript Partial Application with Objects
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
// NOTE: Requires Object.assign (you might need a polyfill) | |
Function.prototype.obind = function (ctx, obj) { | |
var f = this | |
var args = arguments | |
return function () { | |
arguments[0] = Object.assign(obj, arguments[0]) | |
return f.apply(ctx, arguments) | |
} | |
} | |
// Example use: | |
var add = function (params) { | |
return params.x + params.y | |
} | |
var add10 = add.obind({ x: 10 }) | |
add10({ y: 20 }) //=> 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment