Map.prototype[@@iterator]()

这篇翻译不完整。请帮忙从英语翻译这篇文章

 

@@iterator属性的初始值与 entries 属性的初始值是同一个函数对象.什么意思呢?这句话其实想说的是,@@iterator属性和 entries 属性都是一个函数,并且是同一个函数,也就是说myMap[Symbol.iterator] === myMap.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]();
//返回的其实是个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 (var v of myMap) {
  console.log(v);
}

规范

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

浏览器兼容

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 36 (36) [1] 未实现 未实现 未实现
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support 未实现 36.0 (36) [1] 未实现 未实现 未实现 (Yes)

[1] From Gecko 17 (Firefox 17 / Thunderbird 17 / SeaMonkey 2.14) to Gecko 26 (Firefox 26 / Thunderbird 26 / SeaMonkey 2.23 / Firefox OS 1.2) the iterator property was used (bug 907077), and from Gecko 27 to Gecko 35 the "@@iterator" placeholder was used. In Gecko 36 (Firefox 36 / Thunderbird 36 / SeaMonkey 2.33), the @@iterator symbol got implemented (bug 918828).

另见

文档标签和贡献者

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