On 7/11/2013 4:37 PM, Andrea Giammarchi wrote:
my problems is that generators were not needed there ... values() as array of values, keys() as array of keys and items() as array of key/value pairs.

This is even **easier**

Generators because you wrote them in PHP ... this, is non sense to me (I am zend certified engineer if you are wondering)

Anyway, problem solved: harmony pages and examples in them are outdated.


Well to be more exact, the harmony page showed an example implementation that used a generator, but that was never required. It was always called for keys/values/entries to return an _iterator_, not a generator. Generator functions are a convenient way to create iterators (becase generators *are* iterators).

As an example, the following two implementations can be treated the same for the purposes of iteration:

    // ES6
    function* entries(obj){
      for (let key in obj) {
        yield [key, obj[key]];
      }
    }

    // ES3-5 version
    function entries(obj){
      var keys = [];
      var index = 0;
      for (keys[index++] in obj);
      var total = index;
      index = 0;
      return {
        next: function(){
          if (index < total) {
            var key = keys[index++];
            return { done: false, value: [key, obj[key]] }
          }
          return { done: true };
        }
      };
    }


In the same way, you can more succinctly demonstrate how Map/Set iterators work using generators, but still could be implemented in ES3.

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to