michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: File Name: 11.1.1.js michael@0: ECMA Section: 11.1.1 The this keyword michael@0: Description: michael@0: michael@0: The this keyword evaluates to the this value of the execution context. michael@0: michael@0: Author: christine@netscape.com michael@0: Date: 12 november 1997 michael@0: */ michael@0: var SECTION = "11.1.1"; michael@0: var VERSION = "ECMA_1"; michael@0: startTest(); michael@0: michael@0: writeHeaderToLog( SECTION + " The this keyword"); michael@0: michael@0: var GLOBAL_OBJECT = this.toString(); michael@0: michael@0: // this in global code and eval(this) in global code should return the global object. michael@0: michael@0: new TestCase( SECTION, michael@0: "Global Code: this.toString()", michael@0: GLOBAL_OBJECT, michael@0: this.toString() ); michael@0: michael@0: new TestCase( SECTION, michael@0: "Global Code: eval('this.toString()')", michael@0: GLOBAL_OBJECT, michael@0: eval('this.toString()') ); michael@0: michael@0: // this in anonymous code called as a function should return the global object. michael@0: michael@0: new TestCase( SECTION, michael@0: "Anonymous Code: var MYFUNC = new Function('return this.toString()'); MYFUNC()", michael@0: GLOBAL_OBJECT, michael@0: eval("var MYFUNC = new Function('return this.toString()'); MYFUNC()") ); michael@0: michael@0: // eval( this ) in anonymous code called as a function should return that function's activation object michael@0: michael@0: new TestCase( SECTION, michael@0: "Anonymous Code: var MYFUNC = new Function('return (eval(\"this.toString()\")'); (MYFUNC()).toString()", michael@0: GLOBAL_OBJECT, michael@0: eval("var MYFUNC = new Function('return eval(\"this.toString()\")'); (MYFUNC()).toString()") ); michael@0: michael@0: // this and eval( this ) in anonymous code called as a constructor should return the object michael@0: michael@0: new TestCase( SECTION, michael@0: "Anonymous Code: var MYFUNC = new Function('this.THIS = this'); ((new MYFUNC()).THIS).toString()", michael@0: "[object Object]", michael@0: eval("var MYFUNC = new Function('this.THIS = this'); ((new MYFUNC()).THIS).toString()") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "Anonymous Code: var MYFUNC = new Function('this.THIS = this'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1", michael@0: true, michael@0: eval("var MYFUNC = new Function('this.THIS = this'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "Anonymous Code: var MYFUNC = new Function('this.THIS = eval(\"this\")'); ((new MYFUNC().THIS).toString()", michael@0: "[object Object]", michael@0: eval("var MYFUNC = new Function('this.THIS = eval(\"this\")'); ((new MYFUNC()).THIS).toString()") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "Anonymous Code: var MYFUNC = new Function('this.THIS = eval(\"this\")'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1", michael@0: true, michael@0: eval("var MYFUNC = new Function('this.THIS = eval(\"this\")'); var FUN1 = new MYFUNC(); FUN1.THIS == FUN1") ); michael@0: michael@0: // this and eval(this) in function code called as a function should return the global object. michael@0: new TestCase( SECTION, michael@0: "Function Code: ReturnThis()", michael@0: GLOBAL_OBJECT, michael@0: ReturnThis() ); michael@0: michael@0: new TestCase( SECTION, michael@0: "Function Code: ReturnEvalThis()", michael@0: GLOBAL_OBJECT, michael@0: ReturnEvalThis() ); michael@0: michael@0: // this and eval(this) in function code called as a contructor should return the object. michael@0: new TestCase( SECTION, michael@0: "var MYOBJECT = new ReturnThis(); MYOBJECT.toString()", michael@0: "[object Object]", michael@0: eval("var MYOBJECT = new ReturnThis(); MYOBJECT.toString()") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var MYOBJECT = new ReturnEvalThis(); MYOBJECT.toString()", michael@0: "[object Object]", michael@0: eval("var MYOBJECT = new ReturnEvalThis(); MYOBJECT.toString()") ); michael@0: michael@0: test(); michael@0: michael@0: function ReturnThis() { michael@0: return this.toString(); michael@0: } michael@0: michael@0: function ReturnEvalThis() { michael@0: return( eval("this.toString()") ); michael@0: }