Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6系列之调用默认Iterator接口的场合 #41

Open
itkasumy opened this issue Mar 15, 2018 · 0 comments
Open

ES6系列之调用默认Iterator接口的场合 #41

itkasumy opened this issue Mar 15, 2018 · 0 comments

Comments

@itkasumy
Copy link
Owner

有一些场合会默认调用iterator接口(即Symbol.iterator方法),除了下文会介绍的for...of循环,还有几个别的场合。

有一些场合会默认调用iterator接口(即Symbol.iterator方法),除了下文会介绍的for...of循环,还有几个别的场合。

解构赋值

对数组和Set结构进行解构赋值时,会默认调用iterator接口。

let set = new Set().add('a').add('b').add('c');
 
let [x,y] = set;
// x='a'; y='b'
 
let [first, ...rest] = set;
// first='a'; rest=['b','c'];

扩展运算符

扩展运算符(...)也会调用默认的iterator接口。

// 例一
var str = 'hello';
[...str] //  ['h','e','l','l','o']
 
// 例二
let arr = ['b', 'c'];
['a', ...arr, 'd']
// ['a', 'b', 'c', 'd']

其他场合

以下场合也会用到默认的iterator接口,可以查阅相关章节。

  • yield*
  • Array.from()
  • Map(), Set(), WeakMap(), WeakSet()
  • Promise.all(), Promise.race()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant