Fwd: Side-effects of some Array methods ...

Garrett Smith dhtmlkitchen at gmail.com
Fri Jun 27 14:07:12 PDT 2008


Back to list ->

On Fri, Jun 27, 2008 at 1:27 PM, Brendan Eich <brendan at mozilla.org> wrote:
> On Jun 27, 2008, at 6:51 AM, Pratap Lakshman (VJ#SDK) wrote:
>
>> The side effect is as follows: if "this" does not have a "length"
>> property, it ends up getting one; if "this" does have a length
>> property, but is not an Array, that "length" property will get
>> updated.
>
> True, and not just for pop -- for all generic Array methods that
> mutate length.
>
>> What is the rationale for this?
>
> I don't recall a detailed rationale on this point, but the pseudo-
> code is simplest without a special case that avoids creating a
> 'length' property if one does not exist, and it's easy to rationalize
> "caveat hacker" -- if you wrote Array.prototype.pop.call(p), you
> wanted to have effects on your pizza ;-). The simpler specs are a
> good thing.
>
> Trying to change this now seems dicey. We don't know if any web
> scripts count on this edge case. Creating length may be a requirement
> because lazy pizza coders left 'length' out, knowing it would be
> created, and then depended on it becoming 0 (e.g., in a < expression
> -- if undefined it would become NaN and the condition would be false).
>
> There does not seem to be any profit in taking risk by changing the
> semantics.

A program that called pop() on an object w/no length would know right
away if it failed.

Silent failure would also occur with a length property that has the
attribute ReadOnly. For example, a String (or Function) object, or a
Host object, such as a NodeList (reasonable use case).

javascript:var p = new String("two");
void(Array.prototype.pop.call(p)); alert(p.length);

javascript:var p = document.childNodes; alert(p.length);
try{void(Array.prototype.pop.call(p)); alert(p.length);} catch(ex)
{alert(ex.name); }

FF: alert 2, then "TypeError"
IE: alert 2, then, Choke on ex.name.
SF: alert 2 twice, No error
Op: alert 1, then "Error"

Should the req be that - length - property be defined on the object
itself (not prototype)? If not, [[Put]] will create it if it doesn't
already exist. Should the - length - property be checked for  either
of:
1) the absence of ReadOnly attribute, or
2) being a property that has a setter

If no check is made, or if a check is made - either way, an error will
occur. The difference is where.

<speculation>
ISTM that if IE is going to support generics on Host objects, and
going along with that hypothetical scenario and further pretending
that Array generics would actually work with Host objects and not be
buggy, then, in that case, the Array generics would probably see more
widespread use.
</speculation>

Now, in the hypothetical scenario, would some built-in algorithmic
safety checks help fail faster? Checks for 1) hasOwnProperty and not
readonly or 2) has a setter


Garrett

>
> /be
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
>



More information about the Es4-discuss mailing list