Skip to content

Instantly share code, notes, and snippets.

@gf3
Created July 9, 2010 16:31
Show Gist options
  • Save gf3/469685 to your computer and use it in GitHub Desktop.
Save gf3/469685 to your computer and use it in GitHub Desktop.
Function#bind for V8/node
/**
* Function#bind(context [, arg1 [, arg2 [, argN]]]) -> Function
* - context (Object): Object context to bind to.
* - arg1 (?): Optional argument to curry.
*
* Bind a function to a given `context`. Optionally curry arguments.
*
* ### Examples
*
* var new_func = my_func.bind(my_object, "no u");
**/
if (typeof Function.prototype.bind !== "function") {
Object.defineProperty(Function.prototype, "bind", {
value: (function(){
var _slice = Array.prototype.slice
return function(context) {
var fn = this
, args = _slice.call(arguments, 1)
if (args.length)
return function() {
return arguments.length
? fn.apply(context, args.concat(_slice.call(arguments)))
: fn.apply(context, args)
}
return function() {
return arguments.length
? fn.apply(context, arguments)
: fn.call(context)
}
}
})()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment