Le 15/03/2014 22:51, C. Scott Ananian a écrit :

It would be nicer to add an Object.entries() method that would return that iterator.

Object.prototype.entries or Object.entries(obj)?

That would be less error prone than adding a default iterator to every object.

The world has survived for-in and its weirdo unchangeable enumerable+proto-climbing rules and that was error prone. Now we can control enumerability of things that are added to the prototype and the proposed default-but-still-overridable semantics is to iterate only over own properties. It's less clear to me that the proposed semantics is error prone.

The world has also evolved to a point where tooling can be written to warn about non-overridden @@iterable property for a given "class" (I feel like it is something TypeScript could do at least).

Even if error prone, I'd be interested to hear about arguments in the sense that the risk outweighs the benefits. Iterable-by-default objects is a nice "battery included" feature.

David

  --scott

On Mar 15, 2014 7:42 AM, "David Bruant" <bruan...@gmail.com <mailto:bruan...@gmail.com>> wrote:

    Le 15/03/2014 01:32, Brandon Benvie a écrit :

        On 3/14/2014 5:16 PM, Mark Volkmann wrote:

            Does ES6 add any new ways to iterate over the values in an
            object?
            I've done a lot of searching, but haven't seen anything.
            I'm wondering if there is something more elegant than this:

            Object.keys(myObj).forEach(function (key) {
              let obj = myObj[key];
              // do something with obj
            });


        Not built in, but ES6 does provide a better story for this
        using generators and for-of:

        ```js
        // using a generator function
        function* entries(obj) {
          for (let key of Object.keys(obj)) {
            yield [key, obj[key]];
          }
        }

        // an alternative version using a generator expression
        function entries(obj) {
          return (for (key of Object.keys(obj)) [key, obj[key]]);
        }

        for (let [key, value] of entries(myObj)) {
          // do something with key|value
        }
        ```

    Currently, there is no default Object.prototype.@@iterator, so
    for-of'ing over an object throws a TypeError which isn't really a
    useful default.
    No having a default @@iterator also makes that Map({a:1, b:2})
    throws which is unfortunate.

    Should what you just wrote be made the default
    Object.prototype.@@iterator? It is compatible with the signature
    the Map constructor expects too.

    David
    _______________________________________________
    es-discuss mailing list
    es-discuss@mozilla.org <mailto:es-discuss@mozilla.org>
    https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to