Array.forEach() does not invoke callback for unassigned array elements - fix documentation

RESOLVED FIXED

Status

()

Core
JavaScript Engine
RESOLVED FIXED
11 years ago
11 years ago

People

(Reporter: mharder319, Unassigned)

Tracking

Trunk
x86
Windows Server 2003
Points:
---
Bug Flags:
in-testsuite -

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

11 years ago
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4

From the documentation for Array.forEach():

forEach executes the provided function (callback) once for each element present in the array. Arrays are treated as "dense", meaning that callback will be invoked for each index less than the array's length, even if a given index has never been explicitly assigned.

[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach]

However, the implementation of Array.forEach() does not actually invoke the callback for unassigned array elements.

Reproducible: Always

Steps to Reproduce:
Run the following JavaScript:

var a = [];
a[1] = 1;
a.forEach(alert);

Actual Results:  
alert is called once, with a parameter of "1".

Expected Results:  
alert should be called twice, first with a parameter of "undefined", and then with a parameter of "1".
I think this is a doc bug.  Holes in arrays are not properties, so any primitive method that calls a function "for each property" in the array should not call for those ids that index holes (i.e., for missing properties).  Consider a js shell example:

js> a = [1,2,3]
1,2,3
js> a.forEach(print)
1 0 1,2,3
2 1 1,2,3
3 2 1,2,3
js> delete a[1]
true
js> a.forEach(print)
1 0 1,,3
3 2 1,,3

It would be odd to see a line for item 2 (element 1) after it was deleted, especially in light of the |this| value for the other two lines (1,,3).

Shaver, can you render a verdict?

/be
(In reply to comment #1)
> I think this is a doc bug.

More like a spec bug, in that it documents what was intentional behaviour, but I now agree that it's not desirable.

Do we have compatibility issues here if we change it now?
I'll keep watching this bug for a decision on whether the doc or the implementation gets changed.
I agree with Brendan here: this should be a documentation bug.  I've updated the MDC docs for all the array extras to indicate that they do not act upon array indexes which have not been assigned or which have been deleted (at the time of being visited).  Also, the JS implementations I plan to add to each page (for use when the user's browser doesn't support the extras) will obey this rule as well.
So, does that mean we can resolve this bug, or is there more left to be done?
I'm marking this fixed now, since it seems to be.  If anyone disagrees, please reopen and say what's still not done so we can get it taken care of.
Status: UNCONFIRMED → RESOLVED
Last Resolved: 11 years ago
Resolution: --- → FIXED

Comment 7

11 years ago
see bug 390592 for the test and bug 311082 for the discussion.
Flags: in-testsuite-

Comment 8

11 years ago
(In reply to comment #7)
> see bug 390592 for the test and bug 311082 for the discussion.
The first bug # is wrong.
Summary: Array.forEach() does not invoke callback for unassigned array elements → Array.forEach() does not invoke callback for unassigned array elements - fix documentation

Comment 9

11 years ago
(In reply to comment #8)
see bug 290592 for the test and bug 311082 for the discussion.
You need to log in before you can comment on or make changes to this bug.