3
3
/* eslint no-invalid-this: 1 */
4
4
5
5
var ERROR_MESSAGE = 'Function.prototype.bind called on incompatible ' ;
6
- var slice = Array . prototype . slice ;
7
6
var toStr = Object . prototype . toString ;
7
+ var max = Math . max ;
8
8
var funcType = '[object Function]' ;
9
9
10
+ var concatty = function concatty ( a , b ) {
11
+ var arr = [ ] ;
12
+
13
+ for ( var i = 0 ; i < a . length ; i += 1 ) {
14
+ arr [ i ] = a [ i ] ;
15
+ }
16
+ for ( var j = 0 ; j < b . length ; j += 1 ) {
17
+ arr [ j + a . length ] = b [ j ] ;
18
+ }
19
+
20
+ return arr ;
21
+ } ;
22
+
23
+ var slicy = function slicy ( arrLike , offset ) {
24
+ var arr = [ ] ;
25
+ for ( var i = offset || 0 , j = 0 ; i < arrLike . length ; i += 1 , j += 1 ) {
26
+ arr [ j ] = arrLike [ i ] ;
27
+ }
28
+ return arr ;
29
+ } ;
30
+
31
+ var joiny = function ( arr , joiner ) {
32
+ var str = '' ;
33
+ for ( var i = 0 ; i < arr . length ; i += 1 ) {
34
+ str += arr [ i ] ;
35
+ if ( i + 1 < arr . length ) {
36
+ str += joiner ;
37
+ }
38
+ }
39
+ return str ;
40
+ } ;
41
+
10
42
module . exports = function bind ( that ) {
11
43
var target = this ;
12
- if ( typeof target !== 'function' || toStr . call ( target ) !== funcType ) {
44
+ if ( typeof target !== 'function' || toStr . apply ( target ) !== funcType ) {
13
45
throw new TypeError ( ERROR_MESSAGE + target ) ;
14
46
}
15
- var args = slice . call ( arguments , 1 ) ;
47
+ var args = slicy ( arguments , 1 ) ;
16
48
17
49
var bound ;
18
50
var binder = function ( ) {
19
51
if ( this instanceof bound ) {
20
52
var result = target . apply (
21
53
this ,
22
- args . concat ( slice . call ( arguments ) )
54
+ concatty ( args , arguments )
23
55
) ;
24
56
if ( Object ( result ) === result ) {
25
57
return result ;
@@ -28,18 +60,18 @@ module.exports = function bind(that) {
28
60
}
29
61
return target . apply (
30
62
that ,
31
- args . concat ( slice . call ( arguments ) )
63
+ concatty ( args , arguments )
32
64
) ;
33
65
34
66
} ;
35
67
36
- var boundLength = Math . max ( 0 , target . length - args . length ) ;
68
+ var boundLength = max ( 0 , target . length - args . length ) ;
37
69
var boundArgs = [ ] ;
38
70
for ( var i = 0 ; i < boundLength ; i ++ ) {
39
- boundArgs . push ( '$' + i ) ;
71
+ boundArgs [ i ] = '$' + i ;
40
72
}
41
73
42
- bound = Function ( 'binder' , 'return function (' + boundArgs . join ( ',' ) + '){ return binder.apply(this,arguments); }' ) ( binder ) ;
74
+ bound = Function ( 'binder' , 'return function (' + joiny ( boundArgs , ',' ) + '){ return binder.apply(this,arguments); }' ) ( binder ) ;
43
75
44
76
if ( target . prototype ) {
45
77
var Empty = function Empty ( ) { } ;
0 commit comments