LearningRecord icon indicating copy to clipboard operation
LearningRecord copied to clipboard

可迭代对象有什么特点

Open Rashomon511 opened this issue 5 years ago • 0 comments

image

function makeIterator(array){
    var nextIndex = 0;
    return {
       next: function(){
           return nextIndex < array.length ?
               {value: array[nextIndex++], done: false} :
               {done: true};
       }
    };
}
// 使用 next 方法依次访问对象中的键值
var it = makeIterator(['step', 'by','step']);
console.log(it.next().value); // 'step'
console.log(it.next().value); // 'by'
console.log(it.next().value); // 'step'
console.log(it.next().value);  // undefined
console.log(it.next().done); // true

Rashomon511 avatar Jun 18 '19 01:06 Rashomon511