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: 03 June 2002 michael@0: * SUMMARY: Function param or local var with same name as a function property michael@0: * michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=137000 michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=138708 michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=150032 michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=150859 michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 137000; michael@0: var summary = 'Function param or local var with same name as a function prop'; 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: * Note use of 'x' both for the parameter to f, michael@0: * and as a property name for |f| as an object michael@0: */ michael@0: function f(x) michael@0: { michael@0: } michael@0: michael@0: status = inSection(1); michael@0: f.x = 12; michael@0: actual = f.x; michael@0: expect = 12; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: /* michael@0: * A more elaborate example, using the call() method michael@0: * to chain constructors from child to parent. michael@0: * michael@0: * The key point is the use of the same name 'p' for both michael@0: * the parameter to the constructor, and as a property name michael@0: */ michael@0: function parentObject(p) michael@0: { michael@0: this.p = 1; michael@0: } michael@0: michael@0: function childObject() michael@0: { michael@0: parentObject.call(this); michael@0: } michael@0: childObject.prototype = parentObject; michael@0: michael@0: status = inSection(2); michael@0: var objParent = new parentObject(); michael@0: actual = objParent.p; michael@0: expect = 1; michael@0: addThis(); michael@0: michael@0: status = inSection(3); michael@0: var objChild = new childObject(); michael@0: actual = objChild.p; michael@0: expect = 1; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: /* michael@0: * A similar set-up. Here the same name is being used for michael@0: * the parameter to both the Base and Child constructors, michael@0: */ michael@0: function Base(id) michael@0: { michael@0: } michael@0: michael@0: function Child(id) michael@0: { michael@0: this.prop = id; michael@0: } michael@0: Child.prototype=Base; michael@0: michael@0: status = inSection(4); michael@0: var c1 = new Child('child1'); michael@0: actual = c1.prop; michael@0: expect = 'child1'; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: /* michael@0: * Use same identifier as a property name, too - michael@0: */ michael@0: function BaseX(id) michael@0: { michael@0: } michael@0: michael@0: function ChildX(id) michael@0: { michael@0: this.id = id; michael@0: } michael@0: ChildX.prototype=BaseX; michael@0: michael@0: status = inSection(5); michael@0: c1 = new ChildX('child1'); michael@0: actual = c1.id; michael@0: expect = 'child1'; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: /* michael@0: * From http://bugzilla.mozilla.org/show_bug.cgi?id=150032 michael@0: * michael@0: * Here the same name is being used both for a local variable michael@0: * declared in g(), and as a property name for |g| as an object michael@0: */ michael@0: function g() michael@0: { michael@0: var propA = g.propA; michael@0: var propB = g.propC; michael@0: michael@0: this.getVarA = function() {return propA;} michael@0: this.getVarB = function() {return propB;} michael@0: } michael@0: g.propA = 'A'; michael@0: g.propB = 'B'; michael@0: g.propC = 'C'; michael@0: var obj = new g(); michael@0: michael@0: status = inSection(6); michael@0: actual = obj.getVarA(); // this one was returning 'undefined' michael@0: expect = 'A'; michael@0: addThis(); michael@0: michael@0: status = inSection(7); michael@0: actual = obj.getVarB(); // this one is easy; it never failed michael@0: expect = 'C'; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: /* michael@0: * By martin.honnen@gmx.de michael@0: * From http://bugzilla.mozilla.org/show_bug.cgi?id=150859 michael@0: * michael@0: * Here the same name is being used for a local var in F michael@0: * and as a property name for |F| as an object michael@0: * michael@0: * Twist: the property is added via another function. michael@0: */ michael@0: function setFProperty(val) michael@0: { michael@0: F.propA = val; michael@0: } michael@0: michael@0: function F() michael@0: { michael@0: var propA = 'Local variable in F'; michael@0: } michael@0: michael@0: status = inSection(8); michael@0: setFProperty('Hello'); michael@0: actual = F.propA; // this was returning 'undefined' michael@0: expect = 'Hello'; 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