当前位置:网站首页>Iterator Foundation

Iterator Foundation

2022-07-06 07:33:00 Romantic bearded boy

ES6 Before , The only data structures used to represent a group of data are arrays and objects .

const array = [1, 2, 3, 4];
const object = {
    
    a: 1, 
    b: 2, 
    c: 3
}

ES6 in , added Set and Map Two data structures .

One 、 Code case

var it = makeIterator(['a', 'b']);

it.next() // { value: "a", done: false }
it.next() // { value: "b", done: false }
it.next() // { value: undefined, done: true }

function makeIterator(array) {
    
      var nextIndex = 0;
      return {
    
            next: function() {
    
                  if(nextIndex < array.length) {
    
                    return {
     value: array[nextIndex++], done: false }
                }
                return {
     value: undefined, done: true }
            }
      };
}
原网站

版权声明
本文为[Romantic bearded boy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131916509236.html