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




11 years ago
11 years ago


(Reporter: jeresig, Unassigned)


Firefox Tracking Flags

(Not tracked)





11 years ago
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:

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

Comment 2

11 years ago
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?

Comment 4

11 years ago
I don't think this is a bug.  Array.generic(t, ...) is *intended* to be equivalent to, ...).

Comment 5

11 years ago
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.
Last Resolved: 11 years ago
Resolution: --- → INVALID

Comment 7

11 years ago
One thing that irks me: Just because this method is meant to be similar to 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.

Comment 9

11 years ago
I've started a follow-up discussion on the ECMAScript 4 mailing list:
You need to log in before you can comment on or make changes to this bug.