@@iterator 属性的初始值与 entries 属性的初始值是同一个函数对象。

语法

myMap[Symbol.iterator]

返回值

map 的 iterator 函数默认就是 entries() 函数。

示例

使用 [@@iterator]()

var myMap = new Map();
myMap.set('0', 'foo');
myMap.set(1, 'bar');
myMap.set({}, 'baz');

var mapIter = myMap[Symbol.iterator]();
//返回的其实是个generator
console.log(mapIter.next().value); // ["0", "foo"]
console.log(mapIter.next().value); // [1, "bar"]
console.log(mapIter.next().value); // [Object, "baz"]

for..of中使用[@@iterator]() 

var myMap = new Map();
myMap.set('0', 'foo');
myMap.set(1, 'bar');
myMap.set({}, 'baz');

for (const entry of myMap) {
  console.log(entry);
}
// ["0", "foo"]
// [1, "bar"]
// [{}, "baz"]

for (var v of myMap) {
  console.log(v);
}

// 0: foo
// 1: bar
// [Object]: baz

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Map.prototype[@@iterator]()
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
Map.prototype[@@iterator]()
Draft  

浏览器兼容

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes Yes

36

27 — 361 2

17 — 273 4

No Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes 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, DuLinRain
最后编辑者: Ende93,