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: * Date: 06 February 2003 michael@0: * SUMMARY: Using |instanceof| to check if function is called as a constructor michael@0: * michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=192105 michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 192105; michael@0: var summary = 'Using |instanceof| to check if f() is called as constructor'; 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: michael@0: michael@0: /* michael@0: * This function is the heart of the test. It sets the result michael@0: * variable |actual|, which we will compare against |expect|. michael@0: * michael@0: * Note |actual| will be set to |true| or |false| according michael@0: * to whether or not this function is called as a constructor; michael@0: * i.e. whether it is called via the |new| keyword or not - michael@0: */ michael@0: function f() michael@0: { michael@0: actual = (this instanceof f); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Call f as a constructor from global scope michael@0: */ michael@0: status = inSection(1); michael@0: new f(); // sets |actual| michael@0: expect = true; michael@0: addThis(); michael@0: michael@0: /* michael@0: * Now, not as a constructor michael@0: */ michael@0: status = inSection(2); michael@0: f(); // sets |actual| michael@0: expect = false; michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Call f as a constructor from function scope michael@0: */ michael@0: function F() michael@0: { michael@0: new f(); michael@0: } michael@0: status = inSection(3); michael@0: F(); // sets |actual| michael@0: expect = true; michael@0: addThis(); michael@0: michael@0: /* michael@0: * Now, not as a constructor michael@0: */ michael@0: function G() michael@0: { michael@0: f(); michael@0: } michael@0: status = inSection(4); michael@0: G(); // sets |actual| michael@0: expect = false; michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Now make F() and G() methods of an object michael@0: */ michael@0: var obj = {F:F, G:G}; michael@0: status = inSection(5); michael@0: obj.F(); // sets |actual| michael@0: expect = true; michael@0: addThis(); michael@0: michael@0: status = inSection(6); michael@0: obj.G(); // sets |actual| michael@0: expect = false; michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Now call F() and G() from yet other functions, and use eval() michael@0: */ michael@0: function A() michael@0: { michael@0: eval('F();'); michael@0: } michael@0: status = inSection(7); michael@0: A(); // sets |actual| michael@0: expect = true; michael@0: addThis(); michael@0: michael@0: michael@0: function B() michael@0: { michael@0: eval('G();'); michael@0: } michael@0: status = inSection(8); michael@0: B(); // sets |actual| michael@0: expect = false; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function addThis() 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