1 /* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22 /** 23 File Name: 11.1.1.js 24 ECMA Section: 11.1.1 The this keyword 25 Description: 26 27 The this keyword evaluates to the this value of the execution context. 28 29 Author: christine (at) netscape.com 30 Date: 12 november 1997 31 */ 32 var SECTION = "11.1.1"; 33 var VERSION = "ECMA_1"; 34 startTest(); 35 36 writeHeaderToLog( SECTION + " The this keyword"); 37 38 var testcases = new Array(); 39 var item = 0; 40 41 var GLOBAL_OBJECT = this.toString(); 42 43 // this in global code and eval(this) in global code should return the global object. 44 45 testcases[item++] = new TestCase( SECTION, 46 "Global Code: this.toString()", 47 GLOBAL_OBJECT, 48 this.toString() ); 49 50 testcases[item++] = new TestCase( SECTION, 51 "Global Code: eval('this.toString()')", 52 GLOBAL_OBJECT, 53 eval('this.toString()') ); 54 55 // this in anonymous code called as a function should return the global object. 56 57 testcases[item++] = new TestCase( SECTION, 58 "Anonymous Code: var MYFUNC = new Function('return this.toString()'); MYFUNC()", 59 GLOBAL_OBJECT, 60 eval("var MYFUNC = new Function('return this.toString()'); MYFUNC()") ); 61 62 // eval( this ) in anonymous code called as a function should return that function's activation object 63 64 testcases[item++] = new TestCase( SECTION, 65 "Anonymous Code: var MYFUNC = new Function('return (eval(\"this.toString()\")'); (MYFUNC()).toString()", 66 GLOBAL_OBJECT, 67 eval("var MYFUNC = new Function('return eval(\"this.toString()\")'); (MYFUNC()).toString()") ); 68 69 // this and eval( this ) in anonymous code called as a constructor should return the object 70 71 testcases[item++] = new TestCase( SECTION, 72 "Anonymous Code: var MYFUNC = new Function('this.THIS = this'); ((new MYFUNC()).THIS).toString()", 73 "[object Object]", 74 eval("var MYFUNC = new Function('this.THIS = this'); ((new MYFUNC()).THIS).toString()") ); 75 76 testcases[item++] = new TestCase( SECTION, 77 "Anonymous Code: var MYFUNC = new Function('this.THIS = this'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1", 78 true, 79 eval("var MYFUNC = new Function('this.THIS = this'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1") ); 80 81 testcases[item++] = new TestCase( SECTION, 82 "Anonymous Code: var MYFUNC = new Function('this.THIS = eval(\"this\")'); ((new MYFUNC().THIS).toString()", 83 "[object Object]", 84 eval("var MYFUNC = new Function('this.THIS = eval(\"this\")'); ((new MYFUNC()).THIS).toString()") ); 85 86 testcases[item++] = new TestCase( SECTION, 87 "Anonymous Code: var MYFUNC = new Function('this.THIS = eval(\"this\")'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1", 88 true, 89 eval("var MYFUNC = new Function('this.THIS = eval(\"this\")'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1") ); 90 91 // this and eval(this) in function code called as a function should return the global object. 92 testcases[item++] = new TestCase( SECTION, 93 "Function Code: ReturnThis()", 94 GLOBAL_OBJECT, 95 ReturnThis() ); 96 97 testcases[item++] = new TestCase( SECTION, 98 "Function Code: ReturnEvalThis()", 99 GLOBAL_OBJECT, 100 ReturnEvalThis() ); 101 102 // this and eval(this) in function code called as a contructor should return the object. 103 testcases[item++] = new TestCase( SECTION, 104 "var MYOBJECT = new ReturnThis(); MYOBJECT.toString()", 105 "[object Object]", 106 eval("var MYOBJECT = new ReturnThis(); MYOBJECT.toString()") ); 107 108 testcases[item++] = new TestCase( SECTION, 109 "var MYOBJECT = new ReturnEvalThis(); MYOBJECT.toString()", 110 "[object Object]", 111 eval("var MYOBJECT = new ReturnEvalThis(); MYOBJECT.toString()") ); 112 113 114 115 test(); 116 117 function test() { 118 for ( tc=0; tc < testcases.length; tc++ ) { 119 testcases[tc].passed = writeTestCaseResult( 120 testcases[tc].expect, 121 testcases[tc].actual, 122 testcases[tc].description +" = "+ 123 testcases[tc].actual ); 124 125 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 126 } 127 stopTest(); 128 return ( testcases ); 129 } 130 function ReturnThis() { 131 return this.toString(); 132 } 133 function ReturnEvalThis() { 134 return( eval("this.toString()") ); 135 }