1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
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]';
|
9 |
|
10 | module.exports = function bind(that) {
|
11 | var target = this;
|
12 | if (typeof target !== 'function' || toStr.call(target) !== funcType) {
|
13 | throw new TypeError(ERROR_MESSAGE + target);
|
14 | }
|
15 | var args = slice.call(arguments, 1);
|
16 |
|
17 | var bound;
|
18 | var binder = function () {
|
19 | if (this instanceof bound) {
|
20 | var result = target.apply(
|
21 | this,
|
22 | args.concat(slice.call(arguments))
|
23 | );
|
24 | if (Object(result) === result) {
|
25 | return result;
|
26 | }
|
27 | return this;
|
28 | } else {
|
29 | return target.apply(
|
30 | that,
|
31 | args.concat(slice.call(arguments))
|
32 | );
|
33 | }
|
34 | };
|
35 |
|
36 | var boundLength = Math.max(0, target.length - args.length);
|
37 | var boundArgs = [];
|
38 | for (var i = 0; i < boundLength; i++) {
|
39 | boundArgs.push('$' + i);
|
40 | }
|
41 |
|
42 | bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this,arguments); }')(binder);
|
43 |
|
44 | if (target.prototype) {
|
45 | var Empty = function Empty() {};
|
46 | Empty.prototype = target.prototype;
|
47 | bound.prototype = new Empty();
|
48 | Empty.prototype = null;
|
49 | }
|
50 |
|
51 | return bound;
|
52 | };
|