W3C home > Mailing lists > Public > whatwg@whatwg.org > April 2010

[whatwg] Adding ECMAScript 5 array extras to HTMLCollection

From: David Bruant <bruant@enseirb-matmeca.fr>
Date: Sun, 25 Apr 2010 01:07:49 -0700
Message-ID: <4BD3F855.1040807@enseirb-matmeca.fr>
Le 25/04/2010 00:39, J Z a ?crit :
>
>
>     I have thought a lot about weirdnesses that people could think
>     about like trying to assign a value to the HTMLCollection
>     (divs[14] = myOtherDiv), but once again, it wouldn't be more
>     allowed than it currently is (I have no idea of what happens
>     today, but if an error is thrown in a for-loop, it should throw an
>     error as well in a call within a forEach).
>
>
> How would destructive methods like `push` or `sort` behave? Would 
> `document.body.childNodes.push(document.createTextNode('foo'))` append 
> text node to a body element? Or would it be a noop?
That is actually a very good point.
It think that the behavior should be exactly the same as "an equivalent 
without array methods". (this point of my proposal would need to be made 
completly explicit for each method)
For push, I think that :

collection.push(document.createTextNode('foo'));

// should behave exactly as :

collection.childNodes[collection.length] = document.createTextNode('foo');

I don't know what is the current behavior, but if it throws an error, 
push should throw the same error. If it appends a text node to the body, 
then push should do the same.

sort is a bunch of :
"tmp = collection[i];
collection[i] = collection[j];
collection[j] = tmp"

The first instruction is harmless, but if the second one would throw an 
error, then let's throw the same error for .sort.

The array.prototype methods are actually "shortcuts" for usual coding 
patterns. I describe forEach here : 
http://longtermlaziness.wordpress.com/2010/04/19/array-foreach-introduction/ 
(by the way, you must be the same Kangax who did the ES5 compat table. 
Thank you very much for this work.)
And I have just described push and sort.

Of course, some patterns (like push, pop, sort, reduce...) may not be 
compatible with the fact that we are dealing with live objects. I think 
that it is fine. The way those patterns were working (throwing error, 
doing nothing, creating a new collection item, whatever) should work the 
same with the "shortcuts". There is no reason to change anything from 
the DOM point of view and behaviors that was happening on the DOM. Just 
"embed the behavior in the shortcut".

Once again, I only want to change the ECMAScript language binding, not 
the DOM interfaces or semantics.

Thank you for your answers,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/attachments/20100425/935254af/attachment.htm>
Received on Sunday, 25 April 2010 01:07:49 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:22 UTC