You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
一个数据结构只要具有Symbol.iterator属性,就可以认为是“可遍历的”(iterable)。Symbol.iterator属性本身是一个函数,就是当前数据结构默认的遍历器生成函数。执行这个函数,就会返回一个遍历器。至于属性名Symbol.iterator,它是一个表达式,返回Symbol对象的iterator属性,这是一个预定义好的、类型为 Symbol 的特殊值,所以要放在方括号内(参见 Symbol 一章)。
1. Iterator(遍历器)的概念
iterator是一种统一的接口机制,用来处理4种数据结构:Array,Object,Set,Map。
任何数据结构只要部署 Iterator 接口,就可以完成遍历操作(即依次处理该数据结构的所有成员)。
其作用:
2. 默认 Iterator 接口
"只要部署 Iterator 接口"的意思是
原生具有Symbol.iterator属性的数据结构有
不具备的是
Object
对象,我们可以手动给实例加上Symbol.iterator属性likeArray可以称之为类似数组对象,具有iterator接口,所有可以遍历。非类似数组的对象虽然也可以遍历,但是并不能取到值了
3. Iterator 接口使用场合
对数组和 Set 结构进行解构赋值时,会默认调用Symbol.iterator方法。
由于数组的遍历会调用遍历器接口,所以任何接受数组作为参数的场合,其实都调用了遍历器接口。下面是一些例子。
The text was updated successfully, but these errors were encountered: