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: * Date: 2001-07-13 michael@0: * michael@0: * SUMMARY: Applying Function.prototype.call to the Function object itself michael@0: * michael@0: * michael@0: * ECMA-262 15.3.4.4 Function.prototype.call (thisArg [,arg1 [,arg2,…] ] ) michael@0: * michael@0: * When applied to the Function object itself, thisArg should be ignored. michael@0: * As explained by Waldemar (waldemar@netscape.com): michael@0: * michael@0: * Function.call(obj, "print(this)") is equivalent to invoking michael@0: * Function("print(this)") with this set to obj. Now, Function("print(this)") michael@0: * is equivalent to new Function("print(this)") (see 15.3.1.1), and the latter michael@0: * ignores the this value that you passed it and constructs a function michael@0: * (which we'll call F) which will print the value of the this that will be michael@0: * passed in when F will be invoked. michael@0: * michael@0: * With the last set of () you're invoking F(), which means you're calling it michael@0: * with no this value. When you don't provide a this value, it defaults to the michael@0: * global object. michael@0: * michael@0: */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = '(none)'; michael@0: var summary = 'Applying Function.prototype.call to the Function object itself'; michael@0: var status = ''; michael@0: var statusitems = []; michael@0: var actual = ''; michael@0: var actualvalues = []; michael@0: var expect= ''; michael@0: var expectedvalues = []; michael@0: var self = this; // capture a reference to the global object michael@0: var cnOBJECT_GLOBAL = self.toString(); michael@0: var cnOBJECT_OBJECT = (new Object).toString(); michael@0: var cnHello = 'Hello'; michael@0: var cnRed = 'red'; michael@0: var objTEST = {color:cnRed}; michael@0: var f = new Function(); michael@0: var g = new Function(); michael@0: michael@0: michael@0: f = Function.call(self, 'return cnHello'); michael@0: g = Function.call(objTEST, 'return cnHello'); michael@0: michael@0: status = 'Section A of test'; michael@0: actual = f(); michael@0: expect = cnHello; michael@0: captureThis(); michael@0: michael@0: status = 'Section B of test'; michael@0: actual = g(); michael@0: expect = cnHello; michael@0: captureThis(); michael@0: michael@0: michael@0: f = Function.call(self, 'return this.toString()'); michael@0: g = Function.call(objTEST, 'return this.toString()'); michael@0: michael@0: status = 'Section C of test'; michael@0: actual = f(); michael@0: expect = cnOBJECT_GLOBAL; michael@0: captureThis(); michael@0: michael@0: status = 'Section D of test'; michael@0: actual = g(); michael@0: expect = cnOBJECT_GLOBAL; michael@0: captureThis(); michael@0: michael@0: michael@0: f = Function.call(self, 'return this.color'); michael@0: g = Function.call(objTEST, 'return this.color'); michael@0: michael@0: status = 'Section E of test'; michael@0: actual = f(); michael@0: expect = undefined; michael@0: captureThis(); michael@0: michael@0: status = 'Section F of test'; michael@0: actual = g(); michael@0: expect = undefined; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: function captureThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = actual; michael@0: expectedvalues[UBound] = expect; michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: for (var i = 0; i < UBound; i++) michael@0: { michael@0: reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); michael@0: } michael@0: michael@0: exitFunc ('test'); michael@0: }