Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathimplementation.js
More file actions
Latest commit
84 lines (70 loc) · 2 KB
/
Copy pathimplementation.js
File metadata and controls
84 lines (70 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
/* eslint no-invalid-this: 1 */
varERROR_MESSAGE='Function.prototype.bind called on incompatible ';
vartoStr=Object.prototype.toString;
varmax=Math.max;
varfuncType='[object Function]';
varconcatty=functionconcatty(a,b){
vararr=[];
for(vari=0;i<a.length;i+=1){
arr[i]=a[i];
}
for(varj=0;j<b.length;j+=1){
arr[j+a.length]=b[j];
}
returnarr;
};
varslicy=functionslicy(arrLike,offset){
vararr=[];
for(vari=offset||0,j=0;i<arrLike.length;i+=1,j+=1){
arr[j]=arrLike[i];
}
returnarr;
};
varjoiny=function(arr,joiner){
varstr='';
for(vari=0;i<arr.length;i+=1){
str+=arr[i];
if(i+1<arr.length){
str+=joiner;
}
}
returnstr;
};
module.exports=functionbind(that){
vartarget=this;
if(typeoftarget!=='function'||toStr.apply(target)!==funcType){
thrownewTypeError(ERROR_MESSAGE+target);
}
varargs=slicy(arguments,1);
varbound;
varbinder=function(){
if(thisinstanceofbound){
varresult=target.apply(
this,
concatty(args,arguments)
);
if(Object(result)===result){
returnresult;
}
returnthis;
}
returntarget.apply(
that,
concatty(args,arguments)
);
};
varboundLength=max(0,target.length-args.length);
varboundArgs=[];
for(vari=0;i<boundLength;i++){
boundArgs[i]='$'+i;
}
bound=Function('binder','return function ('+joiny(boundArgs,',')+'){ return binder.apply(this,arguments); }')(binder);
if(target.prototype){
varEmpty=functionEmpty(){};
Empty.prototype=target.prototype;
bound.prototype=newEmpty();
Empty.prototype=null;
}
returnbound;
};