// Does not work with `new funcA.bind(thisArg, args)` if (!Function.prototype.bind) (function(){ // Is this an error? We are invoking <call.bind> method before it's defined. var slice = Array.prototype.slice.call.bind(Array.prototype.slice); Function.prototype.bind = function() { var thatFunc = this, thatArg = arguments[0]; var args = slice(arguments, 1); if (typeof thatFunc !== 'function') { // closest thing possible to the ECMAScript 5 // internal IsCallable function thrownewTypeError('Function.prototype.bind - ' + 'what is trying to be bound is not callable'); } returnfunction(){ var funcArgs = args.concat(slice(arguments)) return thatFunc.apply(thatArg, funcArgs); }; }; })();
// Yes, it does work with `new funcA.bind(thisArg, args)` if (!Function.prototype.bind) (function(){ var ArrayPrototypeSlice = Array.prototype.slice; Function.prototype.bind = function(otherThis) { if (typeofthis !== 'function') { // closest thing possible to the ECMAScript 5 // internal IsCallable function thrownewTypeError('Function.prototype.bind - what is trying to be bound is not callable'); }
var baseArgs= ArrayPrototypeSlice .call(arguments, 1), baseArgsLength = baseArgs.length, fToBind = this, fNOP = function() {}, fBound = function() { baseArgs.length = baseArgsLength; // reset to default base arguments baseArgs.push.apply(baseArgs, arguments); return fToBind.apply( fNOP.prototype.isPrototypeOf(this) ? this : otherThis, baseArgs ); };
if (this.prototype) { // Function.prototype doesn't have a prototype property fNOP.prototype = this.prototype; } fBound.prototype = new fNOP();