We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

@@iterator 属性和 values() 属性的初始值均为同一个函数对象。

语法

arr[Symbol.iterator]()

返回值

数组的 iterator 方法,默认情况下与 values() 返回值相同

示例

使用 for...of 循环进行迭代

var arr = ['w', 'y', 'k', 'o', 'p'];
var eArr = arr[Symbol.iterator]();
// 您的浏览器必须支持for...of循环
// 以及let —— 将变量作用域限定在 for 循环中
for (let letter of eArr) {
  console.log(letter);
}

另一种迭代方式

var arr = ['w', 'y', 'k', 'o', 'p'];
var eArr = arr[Symbol.iterator]();
console.log(eArr.next().value); // w
console.log(eArr.next().value); // y
console.log(eArr.next().value); // k
console.log(eArr.next().value); // o
console.log(eArr.next().value); // p

规范

规范名称 规范状态 备注
ECMAScript 2015 (6th Edition, ECMA-262)
Array.prototype[@@iterator]()
Standard 首次定义
ECMAScript Latest Draft (ECMA-262)
Array.prototype[@@iterator]()
Draft  

浏览器兼容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support38 ?

36

27 — 361 2

17 — 273 4

No25 Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes ?

36

27 — 361 2

17 — 273 4

Yes Yes Yes

1. A placeholder property named @@iterator is used.

2. Supported as @@iterator.

3. A placeholder property named iterator is used.

4. Supported as iterator.

相关链接

文档标签和贡献者

此页面的贡献者: Ende93, OshotOkill
最后编辑者: Ende93,