| /* eslint no-invalid-this: 1 */ |
| var ERROR_MESSAGE = 'Function.prototype.bind called on incompatible '; |
| var slice = Array.prototype.slice; |
| var toStr = Object.prototype.toString; |
| var funcType = '[object Function]'; |
| module.exports = function bind(that) { |
| if (typeof target !== 'function' || toStr.call(target) !== funcType) { |
| throw new TypeError(ERROR_MESSAGE + target); |
| var args = slice.call(arguments, 1); |
| var binder = function () { |
| if (this instanceof bound) { |
| var result = target.apply( |
| args.concat(slice.call(arguments)) |
| if (Object(result) === result) { |
| args.concat(slice.call(arguments)) |
| var boundLength = Math.max(0, target.length - args.length); |
| for (var i = 0; i < boundLength; i++) { |
| bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this,arguments); }')(binder); |
| var Empty = function Empty() {}; |
| Empty.prototype = target.prototype; |
| bound.prototype = new Empty(); |