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 (at) 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 (at) 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 //----------------------------------------------------------------------------- 43 var UBound = 0; 44 var bug = '(none)'; 45 var summary = 'Applying Function.prototype.call to the Function object itself'; 46 var status = ''; 47 var statusitems = []; 48 var actual = ''; 49 var actualvalues = []; 50 var expect= ''; 51 var expectedvalues = []; 52 var self = this; // capture a reference to the global object 53 var cnOBJECT_GLOBAL = self.toString(); 54 var cnOBJECT_OBJECT = (new Object).toString(); 55 var cnHello = 'Hello'; 56 var cnRed = 'red'; 57 var objTEST = {color:cnRed}; 58 var f = new Function(); 59 var g = new Function(); 60 61 62 f = Function.call(self, 'return cnHello'); 63 g = Function.call(objTEST, 'return cnHello'); 64 65 status = 'Section A of test'; 66 actual = f(); 67 expect = cnHello; 68 captureThis(); 69 70 status = 'Section B of test'; 71 actual = g(); 72 expect = cnHello; 73 captureThis(); 74 75 76 f = Function.call(self, 'return this.toString()'); 77 g = Function.call(objTEST, 'return this.toString()'); 78 79 status = 'Section C of test'; 80 actual = f(); 81 expect = cnOBJECT_GLOBAL; 82 captureThis(); 83 84 status = 'Section D of test'; 85 actual = g(); 86 expect = cnOBJECT_GLOBAL; 87 captureThis(); 88 89 90 f = Function.call(self, 'return this.color'); 91 g = Function.call(objTEST, 'return this.color'); 92 93 status = 'Section E of test'; 94 actual = f(); 95 expect = undefined; 96 captureThis(); 97 98 status = 'Section F of test'; 99 actual = g(); 100 expect = undefined; 101 captureThis(); 102 103 104 105 //----------------------------------------------------------------------------- 106 test(); 107 //----------------------------------------------------------------------------- 108 109 110 function captureThis() 111 { 112 statusitems[UBound] = status; 113 actualvalues[UBound] = actual; 114 expectedvalues[UBound] = expect; 115 UBound++; 116 } 117 118 119 function 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