Closed Bug 424411 Opened 16 years ago Closed 16 years ago

Array.forEach(null, ...) loops over iframes

Categories

(Core :: DOM: Core & HTML, defect)

defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: jeresig, Unassigned)

References

()

Details

Typically doing:

  Array.forEach(null, function(){alert(arguments);});

produces no alerts. However, performing it in a page that has an iframe will cause it to loop over all the iframes in the page, as in this test case:

  http://ejohn.org/files/bugs/foreach/

It wasn't clear if this was a bug with the JavaScript Engine - or only within the DOM - but I figured that this issue might be systemic.
DOM, if not WONTFIX -- forEach just wants to make sure there's a .length property, and then it runs on every property that's present between 0 and .length - 1, inclusive.

(I do wonder where it's finding .length, though!)
Assignee: general → nobody
Component: JavaScript Engine → DOM
QA Contact: general → general
The iframes are window[0], window[1], etc.  Does it makes sense that the script in comment 0 is equivalent to running forEach over the global (window) object?
Yes, a function applied to null gets the global object.  Still wonder where length is coming from, kinda -- maybe some DOM0 hangover?
I don't think this is a bug.  Array.generic(t, ...) is *intended* to be equivalent to Array.prototype.generic.call(t, ...).
It makes sense to be able to use window.frames.length, window.frames[0], etc.  But window.frames is the same as window, so these also show up as window.length, window[0], etc.
Bingo.
Status: NEW → RESOLVED
Closed: 16 years ago
Resolution: --- → INVALID
One thing that irks me: Just because this method is meant to be similar to Array.prototype.forEach.call(null) doesn't mean we have to keep the crazy, non-intuitive, non-deterministic quirks associated with it.

To me acceptable solutions would be:
 - Do nothing (ignore the null, don't loop).
 - Throw an exception (it's not an object, don't treat it as such).
What's non-deterministic about it?  Are you seeing behaviour that varies from call to call, or based on changes that aren't used as specified inputs or outputs?

null isn't an object when it's used in call or apply either, how would the inconsistency be justified?  When you all a function without an explicit this, you get the global object -- for all cases on the web that I know of.

The gotcha here is that window looks like an array, when it has frames, because of the DOM0 hangover.  I don't recommend changing that, either.
I've started a follow-up discussion on the ECMAScript 4 mailing list:
https://mail.mozilla.org/pipermail/es4-discuss/2008-March/002472.html
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.