@@ -16,27 +16,44 @@ var common = uiRouter.common,
16
16
* Because PhantomJS sucks...
17
17
*/
18
18
if ( ! Function . prototype . bind ) {
19
- Function . prototype . bind = function ( oThis ) {
20
- if ( typeof this !== 'function' ) {
21
- // closest thing possible to the ECMAScript 5
22
- // internal IsCallable function
23
- throw new TypeError ( ' Function.prototype.bind - what is trying to be bound is not callable' ) ;
19
+ var Empty = function ( ) { } ;
20
+ Function . prototype . bind = function bind ( that ) { // .length is 1
21
+ var target = this ;
22
+ if ( typeof target != " function" ) {
23
+ throw new TypeError ( " Function.prototype.bind called on incompatible " + target ) ;
24
24
}
25
+ var args = Array . prototype . slice . call ( arguments , 1 ) ; // for normal call
26
+ var binder = function ( ) {
27
+ if ( this instanceof bound ) {
28
+ var result = target . apply (
29
+ this ,
30
+ args . concat ( Array . prototype . slice . call ( arguments ) )
31
+ ) ;
32
+ if ( Object ( result ) === result ) {
33
+ return result ;
34
+ }
35
+ return this ;
36
+ } else {
37
+ return target . apply (
38
+ that ,
39
+ args . concat ( Array . prototype . slice . call ( arguments ) )
40
+ ) ;
41
+ }
42
+ } ;
43
+ var boundLength = Math . max ( 0 , target . length - args . length ) ;
44
+ var boundArgs = [ ] ;
45
+ for ( var i = 0 ; i < boundLength ; i ++ ) {
46
+ boundArgs . push ( "$" + i ) ;
47
+ }
48
+ var bound = Function ( "binder" , "return function(" + boundArgs . join ( "," ) + "){return binder.apply(this,arguments)}" ) ( binder ) ;
25
49
26
- var aArgs = Array . prototype . slice . call ( arguments , 1 ) ,
27
- fToBind = this ,
28
- fNOP = function ( ) { } ,
29
- fBound = function ( ) {
30
- return fToBind . apply ( this instanceof fNOP && oThis
31
- ? this
32
- : oThis ,
33
- aArgs . concat ( Array . prototype . slice . call ( arguments ) ) ) ;
34
- } ;
35
-
36
- fNOP . prototype = this . prototype ;
37
- fBound . prototype = new fNOP ( ) ;
38
-
39
- return fBound ;
50
+ if ( target . prototype ) {
51
+ Empty . prototype = target . prototype ;
52
+ bound . prototype = new Empty ( ) ;
53
+ // Clean up dangling references.
54
+ Empty . prototype = null ;
55
+ }
56
+ return bound ;
40
57
} ;
41
58
}
42
59
0 commit comments