遍历器生成函数(interator.js)

function makeInterator(arr) {
  let nextIndex = 0;
  return {
    next: function() {
      if (nextIndex < arr.length) {
        return {
          value: arr[nextIndex++], done: false
        }
      }else {
        return {
          value: arr[nextIndex++], done: true
        }
      }
    }
  }
}

let testArr = [
  {
    name: '小明', age: 23, job: '教师'
  },
  {
    name: '小红', age: 32, job: '职业经理人'
  },
  {
    name: '小明', age: 28, job: '工程师'
  },
  {
    name: '小明', age: 17, job: 'student'
  }
];
let it = makeInterator(testArr);
for (var a = 0; a <= testArr.length; a++) {
  console.log(it.next());
}

/***
 */

 

你可能感兴趣的:(Javascript基础)