Last Comment Bug 344177 - Array.forEach() does not invoke callback for unassigned array elements - fix documentation
: Array.forEach() does not invoke callback for unassigned array elements - fix ...
Status: RESOLVED FIXED
:
Product: Core
Classification: Components
Component: JavaScript Engine (show other bugs)
: Trunk
: x86 Windows Server 2003
: -- normal (vote)
: ---
Assigned To: general
:
: Jason Orendorff [:jorendorff]
Mentors:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2006-07-10 16:29 PDT by mharder319
Modified: 2007-01-16 10:27 PST (History)
7 users (show)
bob: in‑testsuite-
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Attachments

Description mharder319 2006-07-10 16:29:23 PDT
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".
Comment 1 Brendan Eich [:brendan] 2006-07-10 18:01:01 PDT
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
Comment 2 Mike Shaver (:shaver -- probably not reading bugmail closely) 2006-12-20 07:48:07 PST
(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?
Comment 3 Eric Shepherd [:sheppy] 2006-12-20 13:34:08 PST
I'll keep watching this bug for a decision on whether the doc or the implementation gets changed.
Comment 4 Jeff Walden [:Waldo] (remove +bmo to email) 2006-12-20 13:56:23 PST
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.
Comment 5 Eric Shepherd [:sheppy] 2007-01-02 09:16:56 PST
So, does that mean we can resolve this bug, or is there more left to be done?
Comment 6 Eric Shepherd [:sheppy] 2007-01-08 10:28:28 PST
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.
Comment 7 Bob Clary [:bc:] 2007-01-11 10:35:46 PST
see bug 390592 for the test and bug 311082 for the discussion.
Comment 8 Nickolay_Ponomarev 2007-01-12 08:09:33 PST
(In reply to comment #7)
> see bug 390592 for the test and bug 311082 for the discussion.
The first bug # is wrong.
Comment 9 Bob Clary [:bc:] 2007-01-16 10:27:05 PST
(In reply to comment #8)
see bug 290592 for the test and bug 311082 for the discussion.

Note You need to log in before you can comment on or make changes to this bug.