Closed Bug 252007 Opened 21 years ago Closed 21 years ago

length of arrays is incorrect if a text key is used

Categories

(Core :: JavaScript Engine, defect)

Other Branch
x86
Linux
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: kae, Unassigned)

References

()

Details

User-Agent: Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7) Gecko/20040626 Firefox/0.9.1 Build Identifier: Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7) Gecko/20040626 Firefox/0.9.1 If a text key is used for an array, then the .length property is incorrect. For example; arr['test'] is a valid array element, but it will not be counted for the .length property of an array Reproducible: Always Steps to Reproduce: 1. create an array in javascript 2. assign at least one text key 3. read the array's length Actual Results: The number of items does not include the text element. For example, in this code, the length is reported as 3: test=new Array(); test[test.length]=1; test[test.length]=2; test[test.length]=3; test['test']=4; alert(test.length); strangely, the following reports 3 as well, but that may not be a bug: test=new Array(); test[2]=1; alert(test.length); Expected Results: In the example website, the result should be 1. In the two code snippets above, the values should be 4 and 1 respectively.
--> JavaScript Engine
Component: General → JavaScript Engine
Product: Firefox → Browser
Version: unspecified → Other Branch
Assignee: firefox → general
QA Contact: firefox.general → pschwartau
The length property is defined to only count numeric properties. From http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf (the EcmaScript spec), section 15.4.5.2: length The length property of this Array object is always numerically greater than the name of every property whose name is an array index. The length property has the attributes { DontEnum, DontDelete }. Earlier, from section 15.4 (the "Array Objects" introduction): A property name P (in the form of a string value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^(32)-1. So "test" is not an array index and is not counted in the length. Marking invalid; Mozilla's behavior is exactly correct.
Status: UNCONFIRMED → RESOLVED
Closed: 21 years ago
Resolution: --- → INVALID
*** Bug 285291 has been marked as a duplicate of this bug. ***
If you want to know the number of enumerable (generally, user-defined -- all user-defined properties are enumerable, and most predefined properties are not) properties, SpiderMonkey supports a __count__ property: o = {p:1, q:2}; alert(o.__count__); // alerts 2 /be
You need to log in before you can comment on or make changes to this bug.