在线文档教程

map.@@iterator

map.@@iterator

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

语法

myMap[Symbol.iterator]

返回值

map的 iterator 函数默认就是entries()函数,也就是说直接遍历myMap和遍历myMap.entries()是一样的效果.

示例

使用 [@@iterator]()

var myMap = new Map( myMap.set('0', 'foo' myMap.set(1, 'bar' myMap.set{}, 'baz' var mapIter = myMap[Symbol.iterator]( 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 (var v of myMap) { console.log(v }

规范

SpecificationStatusComment
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Map.prototype@@iterator' in that specification.StandardInitial definition.
ECMAScript Latest Draft (ECMA-262)The definition of 'Map.prototype@@iterator' in that specification.Living Standard

浏览器兼容性

FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support(Yes)36 (36) 1No supportNo supportNo support

FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic supportNo support36.0 (36) 1No supportNo supportNo support(Yes)