ES6迭代器和生成器
一、迭代器 JavaScript 原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6 又添加了Map和Set。这样就需要一种统一的接口机制,来处理所有不同的数据结构。遍历器(Iterator)就是这样一种机制。它是一种接口,为各种不同的数据结构提供统一的访问机制。 任何数据结构只要部署 Iterator 接口,就可以完成遍历操作(即依次处理该数据结构的所有成员) 。 1.Iterator的作用: 为各种数据结构,提供一个统一的、简便的访问接口; 使得数据结构的成员能够按某种次序排列 ES6创造了一种新的遍历命令for...of循环,Iterator接口主要供for...of消费。 2.原生具备iterator接口的数据(可用for of遍历) Array set容器 map容器 String 函数的 arguments 对象 NodeList 对象 let arr3 = [1, 2, 'kobe', true]; for(let i of arr3){ console.log(i); // 1 2 kobe true } let str = 'abcd'; for(let item of str){ console.log(item); // a b c d } function fun() { for (let i of arguments) {