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. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@iterator
|
Chrome 43 | Edge 12 |
Firefox
36
|
IE No | Opera 30 | Safari 10 | WebView Android 43 | Chrome Android 43 |
Firefox Android
36
|
Opera Android 30 | Safari iOS 10 | Samsung Internet Android 4.0 | nodejs 0.12 |
完整支持
不支持
见实现注意事项。
使用非标名称。
Map
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()