@@ -30,9 +30,11 @@ new Test.Unit.Runner({
30
30
} ,
31
31
32
32
testFunctionBind : function ( ) {
33
- function methodWithoutArguments ( ) { return this . hi } ;
34
- function methodWithArguments ( ) { return this . hi + ',' + $A ( arguments ) . join ( ',' ) } ;
33
+ function methodWithoutArguments ( ) { return this . hi ; }
34
+ function methodWithArguments ( ) { return this . hi + ',' + $A ( arguments ) . join ( ',' ) ; }
35
+ function methodReturningContext ( ) { return this ; }
35
36
var func = Prototype . emptyFunction ;
37
+ var u ;
36
38
37
39
// We used to test that `bind` without a `context` argument simply
38
40
// returns the original function, but this contradicts the ES5 spec.
@@ -45,6 +47,10 @@ new Test.Unit.Runner({
45
47
methodWithArguments . bind ( { hi : 'withBindArgs' } , 'arg1' , 'arg2' ) ( ) ) ;
46
48
this . assertEqual ( 'withBindArgsAndArgs,arg1,arg2,arg3,arg4' ,
47
49
methodWithArguments . bind ( { hi : 'withBindArgsAndArgs' } , 'arg1' , 'arg2' ) ( 'arg3' , 'arg4' ) ) ;
50
+
51
+ this . assertEqual ( window , methodReturningContext . bind ( null ) ( ) , 'null has window as its context' ) ;
52
+ this . assertEqual ( window , methodReturningContext . bind ( u ) ( ) , 'undefined has window as its context' ) ;
53
+ this . assertEqual ( '' , methodReturningContext . bind ( '' ) ( ) , 'other falsy values should not have window as their context' ) ;
48
54
49
55
50
56
// Ensure that bound functions ignore their `context` when used as
@@ -133,8 +139,35 @@ new Test.Unit.Runner({
133
139
deferredFunction2 . defer ( 'test' ) ;
134
140
this . wait ( 50 , function ( ) {
135
141
this . assertEqual ( 'test' , window . deferredValue ) ;
142
+
143
+ window . deferBoundProperly = false ;
144
+
145
+ var obj = { foo : 'bar' } ;
146
+ var func = function ( ) {
147
+ if ( 'foo' in this ) window . deferBoundProperly = true ;
148
+ } ;
149
+
150
+ func . bind ( obj ) . defer ( ) ;
151
+
152
+ this . wait ( 50 , function ( ) {
153
+ this . assert ( window . deferBoundProperly ,
154
+ "deferred bound functions should preserve original scope" ) ;
155
+
156
+ window . deferBoundProperlyOnString = false ;
157
+ var str = "<script>window.deferBoundProperlyOnString = true;</script>"
158
+
159
+ str . evalScripts . bind ( str ) . defer ( ) ;
160
+
161
+ this . wait ( 50 , function ( ) {
162
+ this . assert ( window . deferBoundProperlyOnString ) ;
163
+ } ) ;
164
+
165
+ } ) ;
166
+
136
167
} ) ;
137
168
} ) ;
169
+
170
+
138
171
} ,
139
172
140
173
testFunctionMethodize : function ( ) {
0 commit comments