JavaScript Iterators

后端 未结 6 1476
清酒与你
清酒与你 2020-12-28 23:03

I was going through MDN (Mozilla Developer Network) and came across Iterators and generators

So naturally, I tried the snippets of code given in the page on Google C

相关标签:
6条回答
  • 2020-12-28 23:28

    For chrome you could use this

    var someArray = [1, 5, 7];
    var someArrayEntries = someArray.entries();
    

    here is link, which you could find interesting

    0 讨论(0)
  • 2020-12-28 23:33
    var makeIterator = function (collection, property) {
        var agg = (function (collection) {
        var index = 0;
        var length = collection.length;
    
        return {
          next: function () {
            var element;
              if (!this.hasNext()) {
                return null;
              }
              element = collection[index][property];
              index = index + 1;
              return element;
            },
          hasNext: function () {
            return index < length;
          },
          rewind: function () {
            index = 0;
          },
          current: function () {
            return collection[index];
          }
        };
     })(collection);
    
     return agg;
    };
    
    
    var iterator = makeIterator([5,8,4,2]);
    
    console.log(iterator.current())//5
    console.log(  iterator.next() )
    console.log(iterator.current()) //8
    console.log(iterator.rewind());
    console.log(iterator.current()) //5
    
    0 讨论(0)
  • 2020-12-28 23:41

    It means that Chrome v21 does not support that feature of JavaScript. It's part of the 1.7 spec. Trying this might help for specifying explicitly 1.7 support in Chrome.

    0 讨论(0)
  • 2020-12-28 23:42

    From this thread:

    V8 is an implementation of ECMAScript, not JavaScript. The latter is a non-standardized extension of ECMAScript made by Mozilla.

    V8 is intended to be plug-in compatible with JSC, the ECMAScript implementation in WebKit/Safari. As such it implements a number of non-standard extensions of ECMAScript that are also in JSC, and most of these are also in Mozilla's JavaScript languages.

    There is no plan to add non-standard features that are not in JSC to V8.

    Note: JSC stands for JavaScript Core - the WebKit ECMAScript implementation.

    0 讨论(0)
  • 2020-12-28 23:42

    Arrays have a built in map function that acts like an iterator.

    [1,2,3].map(function(input){console.log(input)});
    

    Standard Out:

    1
    2
    3
    

    Worst case you could easily design an iterator object, didn't full test this but if there are any bugs you should be able to quickly get this working.

    var Iterator = function(arr){ return {
        index : -1,
        hasNext : function(){ return this.index <= arr.length; },
        hasPrevious: function(){ return this.index > 0; },
    
        current: function(){ return arr[ this["index"] ]; },
    
        next : function(){
            if(this.hasNext()){
                this.index = this.index + 1;            
                return this.current();
            } 
            return false;
        },
    
        previous : function(){
            if(this.hasPrevious()){
                this.index = this.index - 1
                return this.current();
            }
            return false;
        }
    }   
    };
    
    var iter = Iterator([1,2,3]);
    
    while(iter.hasNext()){
        console.log(iter.next());
    }
    
    0 讨论(0)
  • 2020-12-28 23:50

    window.Iterator AFAIK only exists in Firefox, not WebKit.

    0 讨论(0)
提交回复
热议问题