The initial value of the @@iterator property is the same function object as the initial value of the entries 方法。

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

myMap[Symbol.iterator]
					

返回值

The map iterator function, which is the entries() function by default.

范例

使用 [@@iterator]()

const myMap = new Map()
myMap.set('0', 'foo')
myMap.set(1, 'bar')
myMap.set({}, 'baz')
const 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"]
					

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

const 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 (const [key, value] of myMap) {
  console.log(`${key}: ${value}`)
}
// 0: foo
// 1: bar
// [Object]: baz
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'Map.prototype[@@iterator]()' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
@@iterator Chrome 43 Edge 12 Firefox 36
36
不支持 27 — 36 Alternate Name
A placeholder property named @@iterator 被使用。
Alternate Name Uses the non-standard name: @@iterator
不支持 17 — 27 Alternate Name
A placeholder property named iterator 被使用。
Alternate Name Uses the non-standard name: iterator
IE No Opera 30 Safari 10 WebView Android 43 Chrome Android 43 Firefox Android 36
36
不支持 27 — 36 Alternate Name
A placeholder property named @@iterator 被使用。
Alternate Name Uses the non-standard name: @@iterator
不支持 17 — 27 Alternate Name
A placeholder property named iterator 被使用。
Alternate Name Uses the non-standard name: iterator
Opera Android 30 Safari iOS 10 Samsung Internet Android 4.0 nodejs 0.12

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

使用非标名称。

另请参阅

元数据

  • 最后修改: