Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | /* eslint no-invalid-this: 1 */
|
---|
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 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.