[whatwg] Adding ECMAScript 5 array extras to HTMLCollection

David Bruant bruant at enseirb-matmeca.fr
Sun Apr 25 01:07:49 PDT 2010

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 :


// 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 : 
(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,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/attachments/20100425/935254af/attachment-0002.htm>

More information about the whatwg mailing list