Array extras and Objects

David Bruant bruant.d at gmail.com
Thu Dec 1 05:04:29 PST 2011


Le 01/12/2011 08:47, Andrea Giammarchi a écrit :
> Let's say this is an attempt to bring some new, easy to implement,
> method for the native Object.prototype
> Specially about forEach, the most used Array.prototype method out there,
That's quite an ambitious statement. I use forEach, but I think I use
push, concat, every/some, map and reduce quite often as well and I
wouldn't be able to tell which I use more often (probably push).

> it's quite clear JS developers would like to have similar method to
> iterate over objects, as key:value pairs rather than index:value.
It seems that iterators [1] could do what you need. In this proposal (so
very likely to be in ES.next), you can see this example:
-----
for ([key, val] of items(x)) {
    alert("x." + key + " = " + val);
}
-----

There is even the possibility for you to define your own iterator on
your objects.

> Almost every framework/library out there has an "error prone"
> each(obj, callback) method, able to automagically recognize if the
> passed obj is an array, an arrayLike, or an object.
> A classic failing scenario is a function used as object, where the
> length property won't mean it's an arrayLike entity.
If some library authors think that an array is an object with a 'length'
property, I think that they should learn JavaScript and that is not a
reason to add a language feature.

David

[1] http://wiki.ecmascript.org/doku.php?id=harmony:iterators


More information about the es-discuss mailing list