1/* 2* The contents of this file are subject to the Netscape Public 3* License Version 1.1 (the "License"); you may not use this file 4* except in compliance with the License. You may obtain a copy of 5* the License at http://www.mozilla.org/NPL/ 6* 7* Software distributed under the License is distributed on an 8* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed 9* or implied. See the License for the specific language governing 10* rights and limitations under the License. 11* 12* The Original Code is mozilla.org code. 13* 14* The Initial Developer of the Original Code is Netscape 15* Communications Corporation. Portions created by Netscape are 16* Copyright (C) 1998 Netscape Communications Corporation. 17* All Rights Reserved. 18* 19* Contributor(s): pschwartau@netscape.com 20* Date: 2001-07-13 21* 22* SUMMARY: Applying Function.prototype.call to the Function object itself 23* 24* 25* ECMA-262 15.3.4.4 Function.prototype.call (thisArg [,arg1 [,arg2,�] ] ) 26* 27* When applied to the Function object itself, thisArg should be ignored. 28* As explained by Waldemar (waldemar@netscape.com): 29* 30* Function.call(obj, "print(this)") is equivalent to invoking 31* Function("print(this)") with this set to obj. Now, Function("print(this)") 32* is equivalent to new Function("print(this)") (see 15.3.1.1), and the latter 33* ignores the this value that you passed it and constructs a function 34* (which we'll call F) which will print the value of the this that will be 35* passed in when F will be invoked. 36* 37* With the last set of () you're invoking F(), which means you're calling it 38* with no this value. When you don't provide a this value, it defaults to the 39* global object. 40* 41*/ 42//----------------------------------------------------------------------------- 43var UBound = 0; 44var bug = '(none)'; 45var summary = 'Applying Function.prototype.call to the Function object itself'; 46var status = ''; 47var statusitems = []; 48var actual = ''; 49var actualvalues = []; 50var expect= ''; 51var expectedvalues = []; 52var self = this; // capture a reference to the global object 53var cnOBJECT_GLOBAL = self.toString(); 54var cnOBJECT_OBJECT = (new Object).toString(); 55var cnHello = 'Hello'; 56var cnRed = 'red'; 57var objTEST = {color:cnRed}; 58var f = new Function(); 59var g = new Function(); 60 61 62f = Function.call(self, 'return cnHello'); 63g = Function.call(objTEST, 'return cnHello'); 64 65status = 'Section A of test'; 66actual = f(); 67expect = cnHello; 68captureThis(); 69 70status = 'Section B of test'; 71actual = g(); 72expect = cnHello; 73captureThis(); 74 75 76f = Function.call(self, 'return this.toString()'); 77g = Function.call(objTEST, 'return this.toString()'); 78 79status = 'Section C of test'; 80actual = f(); 81expect = cnOBJECT_GLOBAL; 82captureThis(); 83 84status = 'Section D of test'; 85actual = g(); 86expect = cnOBJECT_GLOBAL; 87captureThis(); 88 89 90f = Function.call(self, 'return this.color'); 91g = Function.call(objTEST, 'return this.color'); 92 93status = 'Section E of test'; 94actual = f(); 95expect = undefined; 96captureThis(); 97 98status = 'Section F of test'; 99actual = g(); 100expect = undefined; 101captureThis(); 102 103 104 105//----------------------------------------------------------------------------- 106test(); 107//----------------------------------------------------------------------------- 108 109 110function captureThis() 111{ 112 statusitems[UBound] = status; 113 actualvalues[UBound] = actual; 114 expectedvalues[UBound] = expect; 115 UBound++; 116} 117 118 119function test() 120{ 121 enterFunc ('test'); 122 printBugNumber (bug); 123 printStatus (summary); 124 125 for (var i = 0; i < UBound; i++) 126 { 127 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 128 } 129 130 exitFunc ('test'); 131} 132