harttle.github.io icon indicating copy to clipboard operation
harttle.github.io copied to clipboard

2018/09/29/es6-iterators

Open utterances-bot opened this issue 7 years ago • 3 comments

ES6 迭代器:Iterator, Iterable 和 Generator | Harttle Land

对集合中每个元素进行处理是很常见的操作,比如数组遍历、对象的属性遍历。以往这些操作是通过 for 循环、.forEach、.map 等方式进行,在 ES6 中直接把迭代放在语言层面进行支持,同时提供定制 for...of 的机制。借由迭代器机制为 M...

https://harttle.land/2018/09/29/es6-iterators.html

utterances-bot avatar Sep 29 '18 14:09 utterances-bot

使用迭代器比之普通的循环有啥好处啊? 话说比如.forEach运行起来速度挺慢的,所以感觉还是for循环比较好?

ReaRuiRay avatar Sep 29 '18 14:09 ReaRuiRay

统一协议呀,可以用 for...of 循环 Map、Set,甚至是自己定义的链表、树什么的。另外 for...of 和 for...in 一样都是语法层的支持,不仅快而且不会像 forEach 那样产生一个新的 function 作用域

harttle avatar Sep 29 '18 16:09 harttle

学习了!

ReaRuiRay avatar Sep 29 '18 16:09 ReaRuiRay