An array with named items is always length 0
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
People
(Reporter: xwfx6vn9xre8, Unassigned)
Details
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
Steps to reproduce:
a=[] // Array []
length: 0
a["foo"] = 123 // 123
a["bar"] = 456 // 456
a // Array []
| bar: 456
| foo: 123
| length: 0
a[0] // undefined
a["foo"] // 123
a.foo // 123
Object.getOwnPropertyNames(a) // Array(3) [ "length", "foo", "bar" ]
Actual results:
The length of the array stays 0 even though it was added two items.
Expected results:
The length should be 2 or throw an error saying something like "invalid operation".
| Reporter | ||
Comment 1•4 years ago
|
||
Sorry the formatting got messed up.
> a=[] // Array []
length: 0
> a["foo"] = 123 // 123
> a["bar"] = 456 // 456
> a // Array []
| bar: 456
| foo: 123
| length: 0
> a[0] // undefined
> a["foo"] // 123
> a.foo // 123
> Object.getOwnPropertyNames(a) // Array(3) [ "length", "foo", "bar" ]
| Reporter | ||
Comment 2•4 years ago
|
||
This also happens in Chrome.
Comment 3•4 years ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::JavaScript Engine' component, and is moving the bug to that component. Please revert this change in case you think the bot is wrong.
Comment 4•4 years ago
|
||
That is expected behavior per the specification.
Array's length reflects the property only when it's numeric (especially, "array index").
https://tc39.es/ecma262/#sec-array-exotic-objects
An Array is an exotic object that gives special treatment to array index property keys (see 6.1.7). A property whose property name is an array index is also called an element. Every Array has a non-configurable "length" property whose value is always a non-negative integral Number whose mathematical value is less than 232. The value of the "length" property is numerically greater than the name of every own property whose name is an array index; whenever an own property of an Array is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever an own property is added whose name is an array index, the value of the "length" property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the value of the "length" property is changed, every own property whose name is an array index whose value is not smaller than the new length is deleted. This constraint applies only to own properties of an Array and is unaffected by "length" or array index properties that may be inherited from its prototypes.
And it allows having non-numeric properties.
Modifying this to throw will likely break the web, so I don't expect that happens.
https://tc39.es/ecma262/#sec-array-exotic-objects-defineownproperty-p-desc
10.4.2.1 [[DefineOwnProperty]] ( P, Desc )
...
If P is "length", then
...
2. Else if P is an array index, then
...
k. Return true.
3. Return OrdinaryDefineOwnProperty(A, P, Desc).
Description
•