Array extras feedback
Allen Wirfs-Brock
Allen.Wirfs-Brock at microsoft.com
Fri Jul 31 08:47:22 PDT 2009
javascript:[].indexOf(2,{valueOf: function() {alert("valueof"); return -1}})
My quick experiments show that Firefox (3.5.1) does the early exit as currently spec'ed by ES5 while Chrome (2.0.172.37), Safari (4.0), and Opera (
9.63) exhibit the side-effect before returning. Note that the actual FF behavior is different from Mozilla's published algorithm which is apparently what the other browsers implemented.
I'm not aware of any articulated pattern for JavaScript functions regarding whether or not arguments should be fully evaluated for side-effects. As there is a divergence among implementations and since we are trying to close down changes to the draft I'm inclined to leave things as currently specified unless some there are strong arguments to support making the change.
Allen
>-----Original Message-----
>From: es-discuss-bounces at mozilla.org [mailto:es-discuss-
>bounces at mozilla.org] On Behalf Of Jeff Walden
>Sent: Thursday, July 30, 2009 4:56 PM
>To: es-discuss at mozilla.org
>Cc: Brendan Eich
>Subject: Array extras feedback
>
>Brendan asked me yesterday to look at and comment on the array extras
>methods as they exist in ES5, since things are being finalized yesterday
>and today. (I'm not sure whether yesterday was the latest to make these
>comments or whether today is also acceptable; unfortunately Black Hat
>proceedings destroyed my day yesterday. So it goes.) Overall they look
>fine; I find myself somewhat amused that the descriptions in the spec
>are apparently copied from MDC with slight modifications, improvements,
>and clarifications. :-)
>
>The biggest change, using SameValue rather than ===, is certainly a good
>change; we should have noticed that when creating the method in the
>first place.
>
>The only real issue I noticed -- a small issue, not especially critical
>but worth noting in passing, was this: indexOf and lastIndexOf return
>early if this array-like object has length zero. In particular they
>return before this step (the only side-effectful step that's short-
>circuited):
>
> If argument fromIndex was passed let n be ToInteger(fromIndex); else
>let n be len.
>
>Thus, when indexOf and lastIndexOf are used, the provided fromIndex is
>converted to an integer -- except when this array is empty. Why should
>this behavior differ when the array has one element and when it has
>zero? I believe it would be more consistent for fromIndex to always be
>converted to integer (assuming the callback is properly callable and
>that the indexOf call otherwise conforms to expected use), even if this
>array is empty.
>
>Anyway, if this doesn't make it in time to be considered, so be it. If
>it does, I'm not arguing particularly vociferously in favor of it, but
>it seems like an unnecessary difference from what we've already
>implemented, and I don't see an obvious motivation for it.
>
>Jeff
>_______________________________________________
>es-discuss mailing list
>es-discuss at mozilla.org
>https://mail.mozilla.org/listinfo/es-discuss
More information about the es-discuss
mailing list