Array Generics and null

Dean Edwards dean at edwards.name
Sat Mar 22 13:32:52 PDT 2008


John Resig wrote:
> The issue here is that Array.forEach(null, ...) maps to
> Array.prototype.forEach.call(null, ...) - and doing a .call() on a
> function produces the global object (which, in a browser like
> Firefox, is equivalent to window.frames - looping over the iframes on
> the page).
> 
> Obviously this issue extends beyond browsers, or this specific
> situation, since this could occur in any situation where the global
> object has a length property.

I'll quote from the base2 mailing list:

Dean Edwards wrote:
 > I think that the problem is that Array generics were added later.
 >
 > From bugzilla:
 >> Array.generic(t, ...) is *intended* to be equivalent to
 >> Array.prototype.generic.call(t,  ...).
 >
 > If generics were considered when first designing the language then the
 > above statement would have been the other way round.
 >
 > e.g.
 >
 > Array.prototype.forEach = function(block, context) {
 >   Array.forEach(this, block, context);
 > }
 >

That's how the ES4 defined built-in works:

prototype function forEach(eacher, thisObj=null) {
    Array.forEach(this, eacher, thisObj);
}

-dean



More information about the Es4-discuss mailing list