|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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: 03 June 2002 |
|
9 * SUMMARY: Function param or local var with same name as a function property |
|
10 * |
|
11 * See http://bugzilla.mozilla.org/show_bug.cgi?id=137000 |
|
12 * See http://bugzilla.mozilla.org/show_bug.cgi?id=138708 |
|
13 * See http://bugzilla.mozilla.org/show_bug.cgi?id=150032 |
|
14 * See http://bugzilla.mozilla.org/show_bug.cgi?id=150859 |
|
15 * |
|
16 */ |
|
17 //----------------------------------------------------------------------------- |
|
18 var UBound = 0; |
|
19 var BUGNUMBER = 137000; |
|
20 var summary = 'Function param or local var with same name as a function prop'; |
|
21 var status = ''; |
|
22 var statusitems = []; |
|
23 var actual = ''; |
|
24 var actualvalues = []; |
|
25 var expect= ''; |
|
26 var expectedvalues = []; |
|
27 |
|
28 |
|
29 /* |
|
30 * Note use of 'x' both for the parameter to f, |
|
31 * and as a property name for |f| as an object |
|
32 */ |
|
33 function f(x) |
|
34 { |
|
35 } |
|
36 |
|
37 status = inSection(1); |
|
38 f.x = 12; |
|
39 actual = f.x; |
|
40 expect = 12; |
|
41 addThis(); |
|
42 |
|
43 |
|
44 |
|
45 /* |
|
46 * A more elaborate example, using the call() method |
|
47 * to chain constructors from child to parent. |
|
48 * |
|
49 * The key point is the use of the same name 'p' for both |
|
50 * the parameter to the constructor, and as a property name |
|
51 */ |
|
52 function parentObject(p) |
|
53 { |
|
54 this.p = 1; |
|
55 } |
|
56 |
|
57 function childObject() |
|
58 { |
|
59 parentObject.call(this); |
|
60 } |
|
61 childObject.prototype = parentObject; |
|
62 |
|
63 status = inSection(2); |
|
64 var objParent = new parentObject(); |
|
65 actual = objParent.p; |
|
66 expect = 1; |
|
67 addThis(); |
|
68 |
|
69 status = inSection(3); |
|
70 var objChild = new childObject(); |
|
71 actual = objChild.p; |
|
72 expect = 1; |
|
73 addThis(); |
|
74 |
|
75 |
|
76 |
|
77 /* |
|
78 * A similar set-up. Here the same name is being used for |
|
79 * the parameter to both the Base and Child constructors, |
|
80 */ |
|
81 function Base(id) |
|
82 { |
|
83 } |
|
84 |
|
85 function Child(id) |
|
86 { |
|
87 this.prop = id; |
|
88 } |
|
89 Child.prototype=Base; |
|
90 |
|
91 status = inSection(4); |
|
92 var c1 = new Child('child1'); |
|
93 actual = c1.prop; |
|
94 expect = 'child1'; |
|
95 addThis(); |
|
96 |
|
97 |
|
98 |
|
99 /* |
|
100 * Use same identifier as a property name, too - |
|
101 */ |
|
102 function BaseX(id) |
|
103 { |
|
104 } |
|
105 |
|
106 function ChildX(id) |
|
107 { |
|
108 this.id = id; |
|
109 } |
|
110 ChildX.prototype=BaseX; |
|
111 |
|
112 status = inSection(5); |
|
113 c1 = new ChildX('child1'); |
|
114 actual = c1.id; |
|
115 expect = 'child1'; |
|
116 addThis(); |
|
117 |
|
118 |
|
119 |
|
120 /* |
|
121 * From http://bugzilla.mozilla.org/show_bug.cgi?id=150032 |
|
122 * |
|
123 * Here the same name is being used both for a local variable |
|
124 * declared in g(), and as a property name for |g| as an object |
|
125 */ |
|
126 function g() |
|
127 { |
|
128 var propA = g.propA; |
|
129 var propB = g.propC; |
|
130 |
|
131 this.getVarA = function() {return propA;} |
|
132 this.getVarB = function() {return propB;} |
|
133 } |
|
134 g.propA = 'A'; |
|
135 g.propB = 'B'; |
|
136 g.propC = 'C'; |
|
137 var obj = new g(); |
|
138 |
|
139 status = inSection(6); |
|
140 actual = obj.getVarA(); // this one was returning 'undefined' |
|
141 expect = 'A'; |
|
142 addThis(); |
|
143 |
|
144 status = inSection(7); |
|
145 actual = obj.getVarB(); // this one is easy; it never failed |
|
146 expect = 'C'; |
|
147 addThis(); |
|
148 |
|
149 |
|
150 |
|
151 /* |
|
152 * By martin.honnen@gmx.de |
|
153 * From http://bugzilla.mozilla.org/show_bug.cgi?id=150859 |
|
154 * |
|
155 * Here the same name is being used for a local var in F |
|
156 * and as a property name for |F| as an object |
|
157 * |
|
158 * Twist: the property is added via another function. |
|
159 */ |
|
160 function setFProperty(val) |
|
161 { |
|
162 F.propA = val; |
|
163 } |
|
164 |
|
165 function F() |
|
166 { |
|
167 var propA = 'Local variable in F'; |
|
168 } |
|
169 |
|
170 status = inSection(8); |
|
171 setFProperty('Hello'); |
|
172 actual = F.propA; // this was returning 'undefined' |
|
173 expect = 'Hello'; |
|
174 addThis(); |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 //----------------------------------------------------------------------------- |
|
180 test(); |
|
181 //----------------------------------------------------------------------------- |
|
182 |
|
183 |
|
184 |
|
185 function addThis() |
|
186 { |
|
187 statusitems[UBound] = status; |
|
188 actualvalues[UBound] = actual; |
|
189 expectedvalues[UBound] = expect; |
|
190 UBound++; |
|
191 } |
|
192 |
|
193 |
|
194 function test() |
|
195 { |
|
196 enterFunc('test'); |
|
197 printBugNumber(BUGNUMBER); |
|
198 printStatus(summary); |
|
199 |
|
200 for (var i=0; i<UBound; i++) |
|
201 { |
|
202 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
|
203 } |
|
204 |
|
205 exitFunc ('test'); |
|
206 } |