3 /* eslint no-invalid-this: 1 */
5 var ERROR_MESSAGE = 'Function.prototype.bind called on incompatible ';
6 var slice = Array.prototype.slice;
7 var toStr = Object.prototype.toString;
8 var funcType = '[object Function]';
10 module.exports = function bind(that) {
12 if (typeof target !== 'function' || toStr.call(target) !== funcType) {
13 throw new TypeError(ERROR_MESSAGE + target);
15 var args = slice.call(arguments, 1);
18 var binder = function () {
19 if (this instanceof bound) {
20 var result = target.apply(
22 args.concat(slice.call(arguments))
24 if (Object(result) === result) {
31 args.concat(slice.call(arguments))
36 var boundLength = Math.max(0, target.length - args.length);
38 for (var i = 0; i < boundLength; i++) {
39 boundArgs.push('$' + i);
42 bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this,arguments); }')(binder);
44 if (target.prototype) {
45 var Empty = function Empty() {};
46 Empty.prototype = target.prototype;
47 bound.prototype = new Empty();
48 Empty.prototype = null;