Skip to content

Instantly share code, notes, and snippets.

@zdying
Created August 12, 2014 06:11
Show Gist options
  • Save zdying/5efbe115104eadabd35a to your computer and use it in GitHub Desktop.
Save zdying/5efbe115104eadabd35a to your computer and use it in GitHub Desktop.
/**
* Function.prototype.bind
*/
if (!isFunc(Function.prototype.bind)){
Function.prototype.bind = function(thisArg){
if(!isFunc(this)){
throw new TypeError("Bind must be called on a function")
}
var slice = [].slice,
self = this,
bindArgs = slice.call(arguments, 1),
newFunc = function(){
self.apply(
this instanceof newFunc ? this : (thisArg || {}),
bindArgs.concat(slice.call(arguments))
)
};
newFunc.prototype = self.prototype;
return newFunc
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment