Skip to content

Instantly share code, notes, and snippets.

@gilbert
Created March 27, 2015 18:59
Show Gist options
  • Save gilbert/be35843f7cabb7235d54 to your computer and use it in GitHub Desktop.
Save gilbert/be35843f7cabb7235d54 to your computer and use it in GitHub Desktop.
JavaScript Partial Application with Objects
// 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