regress-192105.js (2664B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* 7 * 8 * Date: 06 February 2003 9 * SUMMARY: Using |instanceof| to check if function is called as a constructor 10 * 11 * See http://bugzilla.mozilla.org/show_bug.cgi?id=192105 12 * 13 */ 14 //----------------------------------------------------------------------------- 15 var UBound = 0; 16 var BUGNUMBER = 192105; 17 var summary = 'Using |instanceof| to check if f() is called as constructor'; 18 var status = ''; 19 var statusitems = []; 20 var actual = ''; 21 var actualvalues = []; 22 var expect= ''; 23 var expectedvalues = []; 24 25 26 /* 27 * This function is the heart of the test. It sets the result 28 * variable |actual|, which we will compare against |expect|. 29 * 30 * Note |actual| will be set to |true| or |false| according 31 * to whether or not this function is called as a constructor; 32 * i.e. whether it is called via the |new| keyword or not - 33 */ 34 function f() 35 { 36 actual = (this instanceof f); 37 } 38 39 40 /* 41 * Call f as a constructor from global scope 42 */ 43 status = inSection(1); 44 new f(); // sets |actual| 45 expect = true; 46 addThis(); 47 48 /* 49 * Now, not as a constructor 50 */ 51 status = inSection(2); 52 f(); // sets |actual| 53 expect = false; 54 addThis(); 55 56 57 /* 58 * Call f as a constructor from function scope 59 */ 60 function F() 61 { 62 new f(); 63 } 64 status = inSection(3); 65 F(); // sets |actual| 66 expect = true; 67 addThis(); 68 69 /* 70 * Now, not as a constructor 71 */ 72 function G() 73 { 74 f(); 75 } 76 status = inSection(4); 77 G(); // sets |actual| 78 expect = false; 79 addThis(); 80 81 82 /* 83 * Now make F() and G() methods of an object 84 */ 85 var obj = {F:F, G:G}; 86 status = inSection(5); 87 obj.F(); // sets |actual| 88 expect = true; 89 addThis(); 90 91 status = inSection(6); 92 obj.G(); // sets |actual| 93 expect = false; 94 addThis(); 95 96 97 /* 98 * Now call F() and G() from yet other functions, and use eval() 99 */ 100 function A() 101 { 102 eval('F();'); 103 } 104 status = inSection(7); 105 A(); // sets |actual| 106 expect = true; 107 addThis(); 108 109 110 function B() 111 { 112 eval('G();'); 113 } 114 status = inSection(8); 115 B(); // sets |actual| 116 expect = false; 117 addThis(); 118 119 120 121 122 //----------------------------------------------------------------------------- 123 test(); 124 //----------------------------------------------------------------------------- 125 126 127 128 function addThis() 129 { 130 statusitems[UBound] = status; 131 actualvalues[UBound] = actual; 132 expectedvalues[UBound] = expect; 133 UBound++; 134 } 135 136 137 function test() 138 { 139 printBugNumber(BUGNUMBER); 140 printStatus(summary); 141 142 for (var i=0; i<UBound; i++) 143 { 144 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 145 } 146 }