Created
July 9, 2010 16:31
-
-
Save gf3/469685 to your computer and use it in GitHub Desktop.
Function#bind for V8/node
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#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