|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 11.1.1.js |
|
9 ECMA Section: 11.1.1 The this keyword |
|
10 Description: |
|
11 |
|
12 The this keyword evaluates to the this value of the execution context. |
|
13 |
|
14 Author: christine@netscape.com |
|
15 Date: 12 november 1997 |
|
16 */ |
|
17 var SECTION = "11.1.1"; |
|
18 var VERSION = "ECMA_1"; |
|
19 startTest(); |
|
20 |
|
21 writeHeaderToLog( SECTION + " The this keyword"); |
|
22 |
|
23 var GLOBAL_OBJECT = this.toString(); |
|
24 |
|
25 // this in global code and eval(this) in global code should return the global object. |
|
26 |
|
27 new TestCase( SECTION, |
|
28 "Global Code: this.toString()", |
|
29 GLOBAL_OBJECT, |
|
30 this.toString() ); |
|
31 |
|
32 new TestCase( SECTION, |
|
33 "Global Code: eval('this.toString()')", |
|
34 GLOBAL_OBJECT, |
|
35 eval('this.toString()') ); |
|
36 |
|
37 // this in anonymous code called as a function should return the global object. |
|
38 |
|
39 new TestCase( SECTION, |
|
40 "Anonymous Code: var MYFUNC = new Function('return this.toString()'); MYFUNC()", |
|
41 GLOBAL_OBJECT, |
|
42 eval("var MYFUNC = new Function('return this.toString()'); MYFUNC()") ); |
|
43 |
|
44 // eval( this ) in anonymous code called as a function should return that function's activation object |
|
45 |
|
46 new TestCase( SECTION, |
|
47 "Anonymous Code: var MYFUNC = new Function('return (eval(\"this.toString()\")'); (MYFUNC()).toString()", |
|
48 GLOBAL_OBJECT, |
|
49 eval("var MYFUNC = new Function('return eval(\"this.toString()\")'); (MYFUNC()).toString()") ); |
|
50 |
|
51 // this and eval( this ) in anonymous code called as a constructor should return the object |
|
52 |
|
53 new TestCase( SECTION, |
|
54 "Anonymous Code: var MYFUNC = new Function('this.THIS = this'); ((new MYFUNC()).THIS).toString()", |
|
55 "[object Object]", |
|
56 eval("var MYFUNC = new Function('this.THIS = this'); ((new MYFUNC()).THIS).toString()") ); |
|
57 |
|
58 new TestCase( SECTION, |
|
59 "Anonymous Code: var MYFUNC = new Function('this.THIS = this'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1", |
|
60 true, |
|
61 eval("var MYFUNC = new Function('this.THIS = this'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1") ); |
|
62 |
|
63 new TestCase( SECTION, |
|
64 "Anonymous Code: var MYFUNC = new Function('this.THIS = eval(\"this\")'); ((new MYFUNC().THIS).toString()", |
|
65 "[object Object]", |
|
66 eval("var MYFUNC = new Function('this.THIS = eval(\"this\")'); ((new MYFUNC()).THIS).toString()") ); |
|
67 |
|
68 new TestCase( SECTION, |
|
69 "Anonymous Code: var MYFUNC = new Function('this.THIS = eval(\"this\")'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1", |
|
70 true, |
|
71 eval("var MYFUNC = new Function('this.THIS = eval(\"this\")'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1") ); |
|
72 |
|
73 // this and eval(this) in function code called as a function should return the global object. |
|
74 new TestCase( SECTION, |
|
75 "Function Code: ReturnThis()", |
|
76 GLOBAL_OBJECT, |
|
77 ReturnThis() ); |
|
78 |
|
79 new TestCase( SECTION, |
|
80 "Function Code: ReturnEvalThis()", |
|
81 GLOBAL_OBJECT, |
|
82 ReturnEvalThis() ); |
|
83 |
|
84 // this and eval(this) in function code called as a contructor should return the object. |
|
85 new TestCase( SECTION, |
|
86 "var MYOBJECT = new ReturnThis(); MYOBJECT.toString()", |
|
87 "[object Object]", |
|
88 eval("var MYOBJECT = new ReturnThis(); MYOBJECT.toString()") ); |
|
89 |
|
90 new TestCase( SECTION, |
|
91 "var MYOBJECT = new ReturnEvalThis(); MYOBJECT.toString()", |
|
92 "[object Object]", |
|
93 eval("var MYOBJECT = new ReturnEvalThis(); MYOBJECT.toString()") ); |
|
94 |
|
95 test(); |
|
96 |
|
97 function ReturnThis() { |
|
98 return this.toString(); |
|
99 } |
|
100 |
|
101 function ReturnEvalThis() { |
|
102 return( eval("this.toString()") ); |
|
103 } |