From 7267c8c97a1e4d170a0a68e34bc0b3e029f465c7 Mon Sep 17 00:00:00 2001 From: MDN Date: Sat, 17 Jul 2021 00:35:32 +0000 Subject: [CRON] sync translated content --- files/zh-cn/_redirects.txt | 15 + .../global_objects/map/@@iterator/index.html | 104 +++++++ .../global_objects/map/@@species/index.html | 74 +++++ .../global_objects/map/@@tostringtag/index.html | 104 +++++++ .../reference/global_objects/map/clear/index.html | 81 +++++ .../reference/global_objects/map/delete/index.html | 82 +++++ .../global_objects/map/entries/index.html | 79 +++++ .../global_objects/map/foreach/index.html | 111 +++++++ .../reference/global_objects/map/get/index.html | 83 +++++ .../reference/global_objects/map/has/index.html | 123 ++++++++ .../reference/global_objects/map/index.html | 341 +++++++++++++++++++++ .../reference/global_objects/map/keys/index.html | 80 +++++ .../reference/global_objects/map/map/index.html | 65 ++++ .../reference/global_objects/map/set/index.html | 98 ++++++ .../reference/global_objects/map/size/index.html | 79 +++++ .../reference/global_objects/map/values/index.html | 121 ++++++++ .../global_objects/map/@@iterator/index.html | 103 ------- .../global_objects/map/@@species/index.html | 73 ----- .../global_objects/map/@@tostringtag/index.html | 103 ------- .../reference/global_objects/map/clear/index.html | 80 ----- .../reference/global_objects/map/delete/index.html | 81 ----- .../global_objects/map/entries/index.html | 78 ----- .../global_objects/map/foreach/index.html | 110 ------- .../reference/global_objects/map/get/index.html | 82 ----- .../reference/global_objects/map/has/index.html | 122 -------- .../reference/global_objects/map/index.html | 340 -------------------- .../reference/global_objects/map/keys/index.html | 79 ----- .../reference/global_objects/map/map/index.html | 64 ---- .../reference/global_objects/map/set/index.html | 97 ------ .../reference/global_objects/map/size/index.html | 78 ----- .../reference/global_objects/map/values/index.html | 120 -------- 31 files changed, 1640 insertions(+), 1610 deletions(-) create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@iterator/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@species/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@tostringtag/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/clear/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/delete/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/entries/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/foreach/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/get/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/has/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/keys/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/map/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/set/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/size/index.html create mode 100644 files/zh-cn/orphaned/web/javascript/reference/global_objects/map/values/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/@@iterator/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/@@species/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/@@tostringtag/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/clear/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/delete/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/entries/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/foreach/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/get/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/has/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/keys/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/map/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/set/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/size/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/values/index.html diff --git a/files/zh-cn/_redirects.txt b/files/zh-cn/_redirects.txt index ef02ac4a91..da24956a7a 100644 --- a/files/zh-cn/_redirects.txt +++ b/files/zh-cn/_redirects.txt @@ -2062,6 +2062,21 @@ /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/DisplayNames /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/ListFormat /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Locale /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/@@iterator +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/@@species /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/@@species +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/Map /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/Map +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/clear /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/clear +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/delete /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/delete +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/entries /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/entries +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/forEach +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/get /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/get +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/has /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/has +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/keys /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/keys +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/set /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/set +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/size /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/size +/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/values /zh-CN/docs/orphaned/Web/JavaScript/Reference/Global_Objects/Map/values /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/反双曲余弦值 /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/acosh /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/format /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/format diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@iterator/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@iterator/index.html new file mode 100644 index 0000000000..3a0bb5302d --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@iterator/index.html @@ -0,0 +1,104 @@ +--- +title: Map.prototype[@@iterator]() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/@@iterator +tags: + - ECMAScript 2015 + - Iterator + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/@@iterator +original_slug: Web/JavaScript/Reference/Global_Objects/Map/@@iterator +browser-compat: javascript.builtins.Map.@@iterator +--- +
{{JSRef}}
+ +

@@iterator 属性的初始值与 {{jsxref("Map.prototype.entries()", "entries")}} 属性的初始值是同一个函数对象。

+ +
{{EmbedInteractiveExample("pages/js/map-prototype-@@iterator.html")}}
+ + + +

语法

+ +
myMap[Symbol.iterator]
+ +

返回值

+ +

map 的 iterator 函数默认就是 {{jsxref("Map.prototype.entries()", "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
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-map.prototype-@@iterator', 'Map.prototype[@@iterator]()')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype-@@iterator', 'Map.prototype[@@iterator]()')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容

+ + + +

{{Compat("javascript.builtins.Map.@@iterator")}}

+ +

另见

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@species/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@species/index.html new file mode 100644 index 0000000000..4222526fdb --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@species/index.html @@ -0,0 +1,74 @@ +--- +title: get Map[@@species] +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/@@species +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Property +translation_of: Web/JavaScript/Reference/Global_Objects/Map/@@species +original_slug: Web/JavaScript/Reference/Global_Objects/Map/@@species +browser-compat: javascript.builtins.Map.@@species +--- +
{{JSRef}}
+ +

 Map[@@species] 访问器属性会返回一个 Map 构造函数.

+ +

语法

+ +
Map[Symbol.species]
+
+ +

描述

+ +

The species accessor property returns the default constructor for Map objects. Subclass constructors may over-ride it to change the constructor assignment.

+ +

案例

+ +

The species property returns the default constructor function, which is the Map constructor for Map objects:

+ +
Map[Symbol.species]; // function Map()
+ +

In a derived collection object (e.g. your custom map MyMap), the MyMap species is the MyMap constructor. However, you might want to overwrite this, in order to return parent Map objects in your derived class methods:

+ +
class MyMap extends Map {
+  // 重写覆盖 MyMap species to the parent Map constructor
+  static get [Symbol.species]() { return Map; }
+}
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-get-map-@@species', 'get Map [ @@species ]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-get-map-@@species', 'get Map [ @@species ]')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Map.@@species")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@tostringtag/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@tostringtag/index.html new file mode 100644 index 0000000000..9a8dbd4075 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/@@tostringtag/index.html @@ -0,0 +1,104 @@ +--- +title: Map.prototype[@@toStringTag] +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Property + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag +original_slug: Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag +browser-compat: javascript.builtins.Map.@@toStringTag +--- +
{{JSRef}}
+ +

 Map[@@toStringTag] 的初始值是"Map".

+ +
{{js_property_attributes(0,0,1)}}
+ +

语法

+ +
Map[Symbol.toStringTag]
+ +

示例

+ +
Object.prototype.toString.call(new Map()) // "[object Map]"
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-map.prototype-@@tostringtag', 'Map.prototype[@@toStringTag]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype-@@tostringtag', 'Map.prototype[@@toStringTag]')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容

+ +

{{CompatibilityTable}} 

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatChrome(44.0) }}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{ CompatNo }}{{ CompatChrome(44.0) }}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatChrome(44.0)}}
+
diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/clear/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/clear/index.html new file mode 100644 index 0000000000..d617ceb1e0 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/clear/index.html @@ -0,0 +1,81 @@ +--- +title: Map.prototype.clear() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/clear +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/clear +original_slug: Web/JavaScript/Reference/Global_Objects/Map/clear +browser-compat: javascript.builtins.Map.clear +--- +
{{JSRef}}
+ +

clear()方法会移除Map对象中的所有元素。

+ +
{{EmbedInteractiveExample("pages/js/map-prototype-clear.html")}}
+ + + +

语法

+ +
myMap.clear();
+
+ +

返回值

+ +

{{jsxref("undefined")}}.

+ +

示例

+ +

调用clear方法

+ +
var myMap = new Map();
+myMap.set("bar", "baz");
+myMap.set(1, "foo");
+
+myMap.size;       // 2
+myMap.has("bar"); // true
+
+myMap.clear();
+
+myMap.size;       // 0
+myMap.has("bar")  // false
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-map.prototype.clear', 'Map.prototype.clear')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.clear', 'Map.prototype.clear')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容

+ + + +

{{Compat("javascript.builtins.Map.clear")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/delete/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/delete/index.html new file mode 100644 index 0000000000..1f54edc735 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/delete/index.html @@ -0,0 +1,82 @@ +--- +title: Map.prototype.delete() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/delete +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/delete +original_slug: Web/JavaScript/Reference/Global_Objects/Map/delete +browser-compat: javascript.builtins.Map.delete +--- +
{{JSRef}}
+ +

 delete() 方法用于移除 Map 对象中指定的元素。

+ +
{{EmbedInteractiveExample("pages/js/map-prototype-delete.html")}}
+ + + +

语法

+ +
myMap.delete(key);
+ +

参数

+ +
+
key
+
必须。从 Map 对象中移除的元素的键。
+
+ +

返回值

+ +
+
Boolean
+
如果 Map 对象中存在该元素,则移除它并返回 true;否则如果该元素不存在则返回 false
+
+ +

示例

+ +

使用 delete 方法

+ +
var myMap = new Map();
+myMap.set("bar", "foo");
+
+myMap.delete("bar"); // 返回 true。成功地移除元素
+myMap.has("bar");    // 返回 false。"bar" 元素将不再存在于 Map 实例中
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-map.prototype.delete', 'Map.prototype.delete')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.delete', 'Map.prototype.delete')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +

{{Compat("javascript.builtins.Map.delete")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/entries/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/entries/index.html new file mode 100644 index 0000000000..ed3125c008 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/entries/index.html @@ -0,0 +1,79 @@ +--- +title: Map.prototype.entries() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/entries +tags: + - ECMAScript 2015 + - Iterator + - JavaScript + - Map + - Method + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Map/entries +original_slug: Web/JavaScript/Reference/Global_Objects/Map/entries +browser-compat: javascript.builtins.Map.entries +--- +
{{JSRef}}
+ +

entries() 方法返回一个新的包含 [key, value] 对的 Iterator 对象,返回的迭代器的迭代顺序与 Map 对象的插入顺序相同。

+ +

{{EmbedInteractiveExample("pages/js/map-prototype-entries.html")}}

+ +

语法

+ +
myMap.entries()
+
+ +

返回值

+ +

一个新的 {{jsxref("Map")}} 迭代器对象.

+ +

示例

+ +

entries()的使用

+ +
var myMap = new Map();
+myMap.set("0", "foo");
+myMap.set(1, "bar");
+myMap.set({}, "baz");
+
+var mapIter = myMap.entries();
+
+console.log(mapIter.next().value); // ["0", "foo"]
+console.log(mapIter.next().value); // [1, "bar"]
+console.log(mapIter.next().value); // [Object, "baz"]
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-map.prototype.entries', 'Map.prototype.entries')}}{{Spec2('ESDraft')}}
{{SpecName('ES2015', '#sec-map.prototype.entries', 'Map.prototype.entries')}}{{Spec2('ES2015')}}Initial definition.
+ +

浏览器兼容性

+ +

{{Compat("javascript.builtins.Map.entries")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/foreach/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/foreach/index.html new file mode 100644 index 0000000000..a0e057b34b --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/foreach/index.html @@ -0,0 +1,111 @@ +--- +title: Map.prototype.forEach() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/forEach +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/forEach +original_slug: Web/JavaScript/Reference/Global_Objects/Map/forEach +browser-compat: javascript.builtins.Map.forEach +--- +
{{JSRef}}
+ +

forEach() 方法按照插入顺序依次对 Map 中每个键/值对执行一次给定的函数

+ +
{{EmbedInteractiveExample("pages/js/map-prototype-foreach.html")}}
+ + + +

语法

+ +
myMap.forEach(callback([value][,key][,map])[, thisArg])
+ +

参数

+ +
+
callback
+
+

myMap 中每个元素所要执行的函数。它具有如下的参数

+ +
+
value {{Optional_Inline}}
+
每个迭代的值。
+
key {{Optional_Inline}}
+
每个迭代的键。
+
map {{Optional_Inline}}
+
被迭代的map(上文语法框中的 myMap)。
+
+
+
thisArg {{Optional_Inline}}
+
callback 执行中使用的 this 的值。
+
+ +

返回值

+ +

{{jsxref("undefined")}}.

+ +

描述

+ +

forEach 方法会对map中每个真实存在的键执行一次给定的 callback 函数。它不会对被删除的键执行函数。然而,它会对每个值为 undefined 的键执行函数。

+ +

callback 接收三个参数

+ + + +

如果 forEach 中含有 thisArg 参数,那么每次 callback 被调用时,都会被用作 this 的值。否则,undefined 将会被用作 this 的值。按照函数观察到 this 的常用规则callback 函数最终可观察到 this 值。

+ +

每个值只被访问一次,除非它被删除了或者在 forEach 结束前被改变了。callback 不会对在被访问前就删除的元素执行。在 forEach 结束前被添加的元素将会被访问。

+ +

forEach 会对 Map 对象中的每个元素执行一次 callback。它不会返回值。

+ +

示例

+ +

输出一个 Map 对象中的内容

+ +

以下的代码在每行中打印一个 Map 对象中的元素

+ +
function logMapElements(value, key, map) {
+    console.log(`map.get('${key}') = ${value}`)
+}
+new Map([['foo', 3], ['bar', {}], ['baz', undefined]]).forEach(logMapElements)
+// logs:
+// "map.get('foo') = 3"
+// "map.get('bar') = [object Object]"
+// "map.get('baz') = undefined"
+
+ +

规范

+ + + + + + + + + + + + +
规范
{{SpecName('ESDraft', '#sec-map.prototype.foreach', 'Map.prototype.forEach')}}
+ +

浏览器兼容

+ + + +

{{Compat("javascript.builtins.Map.forEach")}}

+ +

参见

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/get/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/get/index.html new file mode 100644 index 0000000000..2ee7875d96 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/get/index.html @@ -0,0 +1,83 @@ +--- +title: Map.prototype.get() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/get +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/get +original_slug: Web/JavaScript/Reference/Global_Objects/Map/get +browser-compat: javascript.builtins.Map.get +--- +
{{JSRef}}
+ +

get() 方法返回某个 Map 对象中的一个指定元素。

+ +
{{EmbedInteractiveExample("pages/js/map-prototype-get.html")}}
+ + + +

语法

+ +
myMap.get(key);
+ +

参数

+ +
+
key
+
必须参数,也是它唯一的参数,要从目标 Map 对象中获取的元素的键。
+
+ +

返回值

+ +

返回一个 Map 对象中与指定键相关联的值,如果找不到这个键则返回 undefined

+ +

示例

+ +

使用 get 方法

+ +
var myMap = new Map();
+myMap.set("bar", "foo");
+
+myMap.get("bar");  // 返回 "foo"
+myMap.get("baz");  // 返回 undefined
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('ES2015', '#sec-map.prototype.get', 'Map.prototype.get')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.get', 'Map.prototype.get')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Map.get")}}

+ +

参见

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/has/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/has/index.html new file mode 100644 index 0000000000..5f76e5fe21 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/has/index.html @@ -0,0 +1,123 @@ +--- +title: Map.prototype.has() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/has +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/has +original_slug: Web/JavaScript/Reference/Global_Objects/Map/has +browser-compat: javascript.builtins.Map.has +--- +
{{JSRef}}
+ +

方法has() 返回一个bool值,用来表明map 中是否存在指定元素.

+ +

语法

+ +
myMap.has(key);
+ +

参数

+ +
+
key
+
必填. 用来检测是否存在指定元素的键值.
+
+ +

返回值

+ +
+
Boolean
+
如果指定元素存在于Map中,则返回true。其他情况返回false
+
+ +

案例

+ +

使用has方法

+ +
var myMap = new Map();
+myMap.set("bar", "foo");
+
+myMap.has("bar");  // returns true
+myMap.has("baz");  // returns false
+
+ +

规范列表

+ + + + + + + + + + + + + + +
规范名称StatusComment
{{SpecName('ES6', '#sec-map.prototype.has', 'Map.prototype.has')}}{{Spec2('ES6')}}Initial definition.
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
浏览器Chrome谷歌Firefox (Gecko)火狐Internet ExplorerOperaSafari
兼容版本38{{CompatGeckoDesktop("13.0")}}11257.1
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
浏览器AndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
兼容版本{{CompatNo}}38{{CompatGeckoMobile("13.0")}}{{CompatNo}}{{CompatNo}}8
+
+ +

相关链接

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/index.html new file mode 100644 index 0000000000..beb4303536 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/index.html @@ -0,0 +1,341 @@ +--- +title: Map +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map +tags: + - Class + - ECMAScript 2015 + - JavaScript + - Map + - Reference + - Polyfill +translation_of: Web/JavaScript/Reference/Global_Objects/Map +original_slug: Web/JavaScript/Reference/Global_Objects/Map +browser-compat: javascript.builtins.Map +--- +
+
{{JSRef}}
+
+ +

Map 对象保存键值对,并且能够记住键的原始插入顺序。任何值(对象或者{{Glossary("Primitive", "原始值")}}) 都可以作为一个键或一个值。

+ +
+
+ +

描述

+ +

一个Map对象在迭代时会根据对象中元素的插入顺序来进行 — 一个  {{jsxref("Statements/for...of", "for...of")}} 循环在每次迭代后会返回一个形式为[key,value]的数组。

+ +

键的相等(Key equality)

+ + + +

Objects 和 maps 的比较

+ +

{{jsxref("Object", "Objects")}} 和 Maps 类似的是,它们都允许你按键存取一个值、删除键、检测一个键是否绑定了值。因此(并且也没有其他内建的替代方式了)过去我们一直都把对象当成 Maps 使用。不过 Maps 和 Objects 有一些重要的区别,在下列情况里使用 Map 会是更好的选择:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
MapObject
意外的键Map 默认情况不包含任何键。只包含显式插入的键。 +

一个 Object 有一个原型, 原型链上的键名有可能和你自己在对象上的设置的键名产生冲突。

+ +
+

注意: 虽然 ES5 开始可以用 Object.create(null) 来创建一个没有原型的对象,但是这种用法不太常见。

+
+
键的类型一个 Map的键可以是任意值,包括函数、对象或任意基本类型。一个Object 的键必须是一个 {{jsxref("String")}} 或是{{jsxref("Symbol")}}。
键的顺序 +

Map 中的 key 是有序的。因此,当迭代的时候,一个 Map 对象以插入的顺序返回键值。

+
+

一个 Object 的键是无序的

+ +
+

注意:自ECMAScript 2015规范以来,对象确实保留了字符串和Symbol键的创建顺序; 因此,在只有字符串键的对象上进行迭代将按插入顺序产生键。

+
+
Size Map 的键值对个数可以轻易地通过{{jsxref("Map.prototype.size", "size")}} 属性获取Object 的键值对个数只能手动计算
迭代Mapiterable 的,所以可以直接被迭代。迭代一个Object需要以某种方式获取它的键然后才能迭代。
性能 +

在频繁增删键值对的场景下表现更好。

+
+

在频繁添加和删除键值对的场景下未作出优化。

+
+ +

构造函数

+ +
+
{{jsxref("Global_Objects/Map/Map", "Map()")}}
+
创建 Map 对象
+
+ +

属性

+ +
+
Map.length
+
属性 length 的值为 0 。
+ 想要计算一个Map 中的条目数量, 使用 {{jsxref("Map.prototype.size")}}.
+
{{jsxref("Map.@@species", "get Map[@@species]")}}
+
本构造函数用于创建派生对象。
+
{{jsxref("Map.prototype")}}
+
表示 Map 构造器的原型。 允许添加属性从而应用于所有的 Map 对象。
+
+ +

Map 实例

+ +

所有的 Map 对象实例都会继承 {{jsxref("Map.prototype")}}。

+ +

属性

+ +

{{page('zh-CN/Web/JavaScript/Reference/Global_Objects/Map/prototype','属性')}}

+ +

方法

+ +

{{page('zh-CN/Web/JavaScript/Reference/Global_Objects/Map/prototype','方法')}}

+ +

示例

+ +

使用 Map 对象

+ +
let myMap = new Map();
+
+let keyObj = {};
+let keyFunc = function() {};
+let keyString = 'a string';
+
+// 添加键
+myMap.set(keyString, "和键'a string'关联的值");
+myMap.set(keyObj, "和键keyObj关联的值");
+myMap.set(keyFunc, "和键keyFunc关联的值");
+
+myMap.size; // 3
+
+// 读取值
+myMap.get(keyString);    // "和键'a string'关联的值"
+myMap.get(keyObj);       // "和键keyObj关联的值"
+myMap.get(keyFunc);      // "和键keyFunc关联的值"
+
+myMap.get('a string');   // "和键'a string'关联的值"
+                         // 因为keyString === 'a string'
+myMap.get({});           // undefined, 因为keyObj !== {}
+myMap.get(function() {}); // undefined, 因为keyFunc !== function () {}
+ +

将 NaN 作为 Map 的键

+ +

NaN 也可以作为Map对象的键。虽然 NaN 和任何值甚至和自己都不相等(NaN !== NaN 返回true),但下面的例子表明,NaN作为Map的键来说是没有区别的:

+ +
let myMap = new Map();
+myMap.set(NaN, "not a number");
+
+myMap.get(NaN); // "not a number"
+
+let otherNaN = Number("foo");
+myMap.get(otherNaN); // "not a number"
+
+ +

使用 for..of 方法迭代 Map

+ +

Map可以使用for..of循环来实现迭代:

+ +
let myMap = new Map();
+myMap.set(0, "zero");
+myMap.set(1, "one");
+for (let [key, value] of myMap) {
+  console.log(key + " = " + value);
+}
+// 将会显示两个log。一个是"0 = zero"另一个是"1 = one"
+
+for (let key of myMap.keys()) {
+  console.log(key);
+}
+// 将会显示两个log。 一个是 "0" 另一个是 "1"
+
+for (let value of myMap.values()) {
+  console.log(value);
+}
+// 将会显示两个log。 一个是 "zero" 另一个是 "one"
+
+for (let [key, value] of myMap.entries()) {
+  console.log(key + " = " + value);
+}
+// 将会显示两个log。 一个是 "0 = zero" 另一个是 "1 = one"
+ +

使用 forEach() 方法迭代 Map

+ +

Map也可以通过forEach()方法迭代:

+ +
myMap.forEach(function(value, key) {
+  console.log(key + " = " + value);
+})
+// 将会显示两个logs。 一个是 "0 = zero" 另一个是 "1 = one"
+
+ +

Map 与数组的关系

+ +
let kvArray = [["key1", "value1"], ["key2", "value2"]];
+
+// 使用常规的Map构造函数可以将一个二维键值对数组转换成一个Map对象
+let myMap = new Map(kvArray);
+
+myMap.get("key1"); // 返回值为 "value1"
+
+// 使用Array.from函数可以将一个Map对象转换成一个二维键值对数组
+console.log(Array.from(myMap)); // 输出和kvArray相同的数组
+
+// 更简洁的方法来做如上同样的事情,使用展开运算符
+console.log([...myMap]);
+
+// 或者在键或者值的迭代器上使用Array.from,进而得到只含有键或者值的数组
+console.log(Array.from(myMap.keys())); // 输出 ["key1", "key2"]
+
+ +

复制或合并 Maps

+ +

Map 能像数组一样被复制:

+ +
let original = new Map([
+  [1, 'one']
+]);
+
+let clone = new Map(original);
+
+console.log(clone.get(1)); // one
+console.log(original === clone); // false. 浅比较 不为同一个对象的引用
+
+ +
+

重要:请记住,数据本身未被克隆。

+
+ +

Map对象间可以进行合并,但是会保持键的唯一性。

+ +
let first = new Map([
+  [1, 'one'],
+  [2, 'two'],
+  [3, 'three'],
+]);
+
+let second = new Map([
+  [1, 'uno'],
+  [2, 'dos']
+]);
+
+// 合并两个Map对象时,如果有重复的键值,则后面的会覆盖前面的。
+// 展开运算符本质上是将Map对象转换成数组。
+let merged = new Map([...first, ...second]);
+
+console.log(merged.get(1)); // uno
+console.log(merged.get(2)); // dos
+console.log(merged.get(3)); // three
+ +

Map对象也能与数组合并:

+ +
let first = new Map([
+  [1, 'one'],
+  [2, 'two'],
+  [3, 'three'],
+]);
+
+let second = new Map([
+  [1, 'uno'],
+  [2, 'dos']
+]);
+
+// Map对象同数组进行合并时,如果有重复的键值,则后面的会覆盖前面的。
+let merged = new Map([...first, ...second, [1, 'eins']]);
+
+console.log(merged.get(1)); // eins
+console.log(merged.get(2)); // dos
+console.log(merged.get(3)); // three
+ +

使用说明

+ +

请注意!为Map设置对象属性也是可以的,但是可能引起大量的混乱。

+ +

所以,你还是可以这样做...

+ +
let wrongMap = new Map()
+wrongMap['bla'] = 'blaa'
+wrongMap['bla2'] = 'blaaa2'
+
+console.log(wrongMap)  // Map { bla: 'blaa', bla2: 'blaaa2' }
+
+ +

...但是,这样做的话,它的行为会不符合预期:

+ +
wrongMap.has('bla')    // false
+wrongMap.delete('bla') // false
+console.log(wrongMap)  // Map { bla: 'blaa', bla2: 'blaaa2' }
+ +

无论如何,和正确用法比较起来,几乎没有什么不同: 

+ +
let myMap = new Map()
+myMap.set('bla','blaa')
+myMap.set('bla2','blaa2')
+console.log(myMap)  // Map { 'bla' => 'blaa', 'bla2' => 'blaa2' }
+
+myMap.has('bla')    // true
+myMap.delete('bla') // true
+console.log(myMap)  // Map { 'bla2' => 'blaa2' }
+ +

规范

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-map-objects', 'Map')}}
+ +

浏览器兼容性

+ +

{{Compat("javascript.builtins.Map")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/keys/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/keys/index.html new file mode 100644 index 0000000000..2907a727b0 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/keys/index.html @@ -0,0 +1,80 @@ +--- +title: Map.prototype.keys() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/keys +tags: + - ECMAScript 2015 + - Iterator + - JavaScript + - Map + - Method + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Map/keys +original_slug: Web/JavaScript/Reference/Global_Objects/Map/keys +browser-compat: javascript.builtins.Map.keys +--- +
{{JSRef}}
+ +

keys() 返回一个引用的 Iterator 对象。它包含按照顺序插入 Map 对象中每个元素的key值。

+ +
{{EmbedInteractiveExample("pages/js/map-prototype-keys.html")}}
+ + + +

语法

+ +
myMap.keys()
+ +

返回值

+ +

一个存在引用关系的 {{jsxref("Map")}} iterator 对象.

+ +

例子

+ +

使用 keys()

+ +
var myMap = new Map();
+myMap.set("0", "foo");
+myMap.set(1, "bar");
+myMap.set({}, "baz");
+
+var mapIter = myMap.keys();
+
+console.log(mapIter.next().value); // "0"
+console.log(mapIter.next().value); // 1
+console.log(mapIter.next().value); // Object
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('ES2015', '#sec-map.prototype.keys', 'Map.prototype.keys')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.keys', 'Map.prototype.keys')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Map.keys")}}

+ +

参见

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/map/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/map/index.html new file mode 100644 index 0000000000..d5b5e525d7 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/map/index.html @@ -0,0 +1,65 @@ +--- +title: Map() 构造函数 +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/Map +tags: + - Constructor + - JavaScript + - Map + - Reference + - Polyfill +translation_of: Web/JavaScript/Reference/Global_Objects/Map/Map +original_slug: Web/JavaScript/Reference/Global_Objects/Map/Map +browser-compat: javascript.builtins.Map.Map +--- +
{{JSRef}}
+ +

Map() 构造函数 创建 {{jsxref("Map")}} 对象.

+ +

语法

+ +
new Map([iterable])
+ +

参数

+ +
+
iterable
+
Iterable 可以是一个{{jsxref("Array", "数组")}}或者其他 iterable 对象,其元素为键值对(两个元素的数组,例如: [[ 1, 'one' ],[ 2, 'two' ]])。 每个键值对都会添加到新的 Map。null 会被当做 undefined。
+
+ +

示例

+ +
let myMap = new Map([
+  [1, 'one'],
+  [2, 'two'],
+  [3, 'three'],
+])
+
+ +

Specifications

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-map-constructor', 'Map constructor')}}
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.Map.Map")}}

+ +

See also

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/set/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/set/index.html new file mode 100644 index 0000000000..4c485fc14f --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/set/index.html @@ -0,0 +1,98 @@ +--- +title: Map.prototype.set() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/set +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/set +original_slug: Web/JavaScript/Reference/Global_Objects/Map/set +browser-compat: javascript.builtins.Map.set +--- +

{{JSRef}}

+ +

set() 方法为 Map 对象添加或更新一个指定了键(key)和值(value)的(新)键值对。

+ +
{{EmbedInteractiveExample("pages/js/map-prototype-set.html")}}
+ + + +

语法

+ +
myMap.set(key, value);
+ +

参数

+ +
+
key
+
要添加至相应 Map 对象的元素的键。
+
value
+
要添加至相应 Map 对象的元素的值。
+
+ +

返回值

+ +

Map 对象

+ +

示例

+ +

使用 set 方法

+ +
var myMap = new Map();
+
+// 将一个新元素添加到 Map 对象
+myMap.set("bar", "foo");
+myMap.set(1, "foobar");
+
+// 在Map对象中更新某个元素的值
+myMap.set("bar", "baz");
+
+ +

链式使用 set 方法

+ +

因为 Set() 方法返回 Map 对象本身,所以你可以像下面这样链式调用它:

+ +
// Add new elements to the map with chaining.
+myMap.set('bar', 'foo')
+     .set(1, 'foobar')
+     .set(2, 'baz');
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('ES2015', '#sec-map.prototype.set', 'Map.prototype.set')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.set', 'Map.prototype.set')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Map.set")}}

+ +

参见

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/size/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/size/index.html new file mode 100644 index 0000000000..bb7ee42b52 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/size/index.html @@ -0,0 +1,79 @@ +--- +title: Map.prototype.size +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/size +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Property +translation_of: Web/JavaScript/Reference/Global_Objects/Map/size +original_slug: Web/JavaScript/Reference/Global_Objects/Map/size +browser-compat: javascript.builtins.Map.size +--- +
{{JSRef}}
+ +

size 是可访问属性,用于返回 一个{{jsxref("Map")}} 对象的成员数量。

+ +

{{EmbedInteractiveExample("pages/js/map-prototype-size.html")}}

+ +

这个示例源码保存在GitHub:https://github.com/mdn/interactive-examples。如果你想贡献代码,修改后在GitHub上发推送请求给我们。

+ +

 

+ +

描述

+ +

size 属性的值是一个整数,表示 Map 对象有多少个键值对。size 是只读属性,用set 方法修改size返回 undefined,即不能改变它的值。

+ +

示例

+ +
var myMap = new Map();
+myMap.set("a", "alpha");
+myMap.set("b", "beta");
+myMap.set("g", "gamma");
+
+myMap.size // 3
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-get-map.prototype.size', 'Map.prototype.size')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-get-map.prototype.size', 'Map.prototype.size')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容

+ +

此页的兼容性表格请查阅:https://github.com/mdn/browser-compat-data 
+ 如果你想更新数据,请在GitHub上给我们发推送请求。

+ +
+

异常提醒

+
+ + + +

相关阅读

+ + diff --git a/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/values/index.html b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/values/index.html new file mode 100644 index 0000000000..2dc91f3c1f --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/global_objects/map/values/index.html @@ -0,0 +1,121 @@ +--- +title: Map.prototype.values() +slug: orphaned/Web/JavaScript/Reference/Global_Objects/Map/values +tags: + - ECMAScript 2015 + - Iterator + - JavaScript + - Map + - Method + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Map/values +original_slug: Web/JavaScript/Reference/Global_Objects/Map/values +browser-compat: javascript.builtins.Map.values +--- +
values() 方法返回一个新的Iterator对象。它包含按顺序插入Map对象中每个元素的value值。
+ +
 
+ +

语法

+ +
myMap.values()
+ +

返回值

+ +

一个新的 Map 可迭代对象.

+ +

例子

+ +

使用 values()

+ +
var myMap = new Map();
+myMap.set("0", "foo");
+myMap.set(1, "bar");
+myMap.set({}, "baz");
+
+var mapIter = myMap.values();
+
+console.log(mapIter.next().value); // "foo"
+console.log(mapIter.next().value); // "bar"
+console.log(mapIter.next().value); // "baz"
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('ES6', '#sec-map.prototype.values', 'Map.prototype.values')}}{{Spec2('ES6')}}初始定义
{{SpecName('ESDraft', '#sec-map.prototype.values', 'Map.prototype.values')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support38{{ CompatGeckoDesktop("20") }}{{CompatNo}}257.1
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}38{{ CompatGeckoMobile("20") }}{{CompatNo}}{{CompatNo}}8
+
+ +

查看

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/@@iterator/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/@@iterator/index.html deleted file mode 100644 index ea20d6b87e..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/@@iterator/index.html +++ /dev/null @@ -1,103 +0,0 @@ ---- -title: Map.prototype[@@iterator]() -slug: Web/JavaScript/Reference/Global_Objects/Map/@@iterator -translation_of: Web/JavaScript/Reference/Global_Objects/Map/@@iterator -tags: - - ECMAScript 2015 - - Iterator - - JavaScript - - Map - - Method - - Prototype - - Reference -browser-compat: javascript.builtins.Map.@@iterator ---- -
{{JSRef}}
- -

@@iterator 属性的初始值与 {{jsxref("Map.prototype.entries()", "entries")}} 属性的初始值是同一个函数对象。

- -
{{EmbedInteractiveExample("pages/js/map-prototype-@@iterator.html")}}
- - - -

语法

- -
myMap[Symbol.iterator]
- -

返回值

- -

map 的 iterator 函数默认就是 {{jsxref("Map.prototype.entries()", "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
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-map.prototype-@@iterator', 'Map.prototype[@@iterator]()')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype-@@iterator', 'Map.prototype[@@iterator]()')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容

- - - -

{{Compat("javascript.builtins.Map.@@iterator")}}

- -

另见

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/@@species/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/@@species/index.html deleted file mode 100644 index f83042c356..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/@@species/index.html +++ /dev/null @@ -1,73 +0,0 @@ ---- -title: get Map[@@species] -slug: Web/JavaScript/Reference/Global_Objects/Map/@@species -translation_of: Web/JavaScript/Reference/Global_Objects/Map/@@species -tags: - - ECMAScript 2015 - - JavaScript - - Map - - Property -browser-compat: javascript.builtins.Map.@@species ---- -
{{JSRef}}
- -

 Map[@@species] 访问器属性会返回一个 Map 构造函数.

- -

语法

- -
Map[Symbol.species]
-
- -

描述

- -

The species accessor property returns the default constructor for Map objects. Subclass constructors may over-ride it to change the constructor assignment.

- -

案例

- -

The species property returns the default constructor function, which is the Map constructor for Map objects:

- -
Map[Symbol.species]; // function Map()
- -

In a derived collection object (e.g. your custom map MyMap), the MyMap species is the MyMap constructor. However, you might want to overwrite this, in order to return parent Map objects in your derived class methods:

- -
class MyMap extends Map {
-  // 重写覆盖 MyMap species to the parent Map constructor
-  static get [Symbol.species]() { return Map; }
-}
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-get-map-@@species', 'get Map [ @@species ]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-get-map-@@species', 'get Map [ @@species ]')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- -
- - -

{{Compat("javascript.builtins.Map.@@species")}}

-
- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/@@tostringtag/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/@@tostringtag/index.html deleted file mode 100644 index cdc56ab916..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/@@tostringtag/index.html +++ /dev/null @@ -1,103 +0,0 @@ ---- -title: Map.prototype[@@toStringTag] -slug: Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag -translation_of: Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag -tags: - - ECMAScript 2015 - - JavaScript - - Map - - Property - - Prototype - - Reference -browser-compat: javascript.builtins.Map.@@toStringTag ---- -
{{JSRef}}
- -

 Map[@@toStringTag] 的初始值是"Map".

- -
{{js_property_attributes(0,0,1)}}
- -

语法

- -
Map[Symbol.toStringTag]
- -

示例

- -
Object.prototype.toString.call(new Map()) // "[object Map]"
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-map.prototype-@@tostringtag', 'Map.prototype[@@toStringTag]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype-@@tostringtag', 'Map.prototype[@@toStringTag]')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容

- -

{{CompatibilityTable}} 

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatChrome(44.0) }}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{ CompatNo }}{{ CompatChrome(44.0) }}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatChrome(44.0)}}
-
diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/clear/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/clear/index.html deleted file mode 100644 index 799c948b22..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/clear/index.html +++ /dev/null @@ -1,80 +0,0 @@ ---- -title: Map.prototype.clear() -slug: Web/JavaScript/Reference/Global_Objects/Map/clear -translation_of: Web/JavaScript/Reference/Global_Objects/Map/clear -tags: - - ECMAScript 2015 - - JavaScript - - Map - - Method - - Prototype - - Reference -browser-compat: javascript.builtins.Map.clear ---- -
{{JSRef}}
- -

clear()方法会移除Map对象中的所有元素。

- -
{{EmbedInteractiveExample("pages/js/map-prototype-clear.html")}}
- - - -

语法

- -
myMap.clear();
-
- -

返回值

- -

{{jsxref("undefined")}}.

- -

示例

- -

调用clear方法

- -
var myMap = new Map();
-myMap.set("bar", "baz");
-myMap.set(1, "foo");
-
-myMap.size;       // 2
-myMap.has("bar"); // true
-
-myMap.clear();
-
-myMap.size;       // 0
-myMap.has("bar")  // false
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-map.prototype.clear', 'Map.prototype.clear')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.clear', 'Map.prototype.clear')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容

- - - -

{{Compat("javascript.builtins.Map.clear")}}

- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/delete/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/delete/index.html deleted file mode 100644 index 2d7bd693ff..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/delete/index.html +++ /dev/null @@ -1,81 +0,0 @@ ---- -title: Map.prototype.delete() -slug: Web/JavaScript/Reference/Global_Objects/Map/delete -translation_of: Web/JavaScript/Reference/Global_Objects/Map/delete -tags: - - ECMAScript 2015 - - JavaScript - - Map - - Method - - Prototype - - Reference -browser-compat: javascript.builtins.Map.delete ---- -
{{JSRef}}
- -

 delete() 方法用于移除 Map 对象中指定的元素。

- -
{{EmbedInteractiveExample("pages/js/map-prototype-delete.html")}}
- - - -

语法

- -
myMap.delete(key);
- -

参数

- -
-
key
-
必须。从 Map 对象中移除的元素的键。
-
- -

返回值

- -
-
Boolean
-
如果 Map 对象中存在该元素,则移除它并返回 true;否则如果该元素不存在则返回 false
-
- -

示例

- -

使用 delete 方法

- -
var myMap = new Map();
-myMap.set("bar", "foo");
-
-myMap.delete("bar"); // 返回 true。成功地移除元素
-myMap.has("bar");    // 返回 false。"bar" 元素将不再存在于 Map 实例中
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES6', '#sec-map.prototype.delete', 'Map.prototype.delete')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.delete', 'Map.prototype.delete')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- -

{{Compat("javascript.builtins.Map.delete")}}

- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/entries/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/entries/index.html deleted file mode 100644 index 0887bfa854..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/entries/index.html +++ /dev/null @@ -1,78 +0,0 @@ ---- -title: Map.prototype.entries() -slug: Web/JavaScript/Reference/Global_Objects/Map/entries -translation_of: Web/JavaScript/Reference/Global_Objects/Map/entries -tags: - - ECMAScript 2015 - - Iterator - - JavaScript - - Map - - Method - - Prototype -browser-compat: javascript.builtins.Map.entries ---- -
{{JSRef}}
- -

entries() 方法返回一个新的包含 [key, value] 对的 Iterator 对象,返回的迭代器的迭代顺序与 Map 对象的插入顺序相同。

- -

{{EmbedInteractiveExample("pages/js/map-prototype-entries.html")}}

- -

语法

- -
myMap.entries()
-
- -

返回值

- -

一个新的 {{jsxref("Map")}} 迭代器对象.

- -

示例

- -

entries()的使用

- -
var myMap = new Map();
-myMap.set("0", "foo");
-myMap.set(1, "bar");
-myMap.set({}, "baz");
-
-var mapIter = myMap.entries();
-
-console.log(mapIter.next().value); // ["0", "foo"]
-console.log(mapIter.next().value); // [1, "bar"]
-console.log(mapIter.next().value); // [Object, "baz"]
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-map.prototype.entries', 'Map.prototype.entries')}}{{Spec2('ESDraft')}}
{{SpecName('ES2015', '#sec-map.prototype.entries', 'Map.prototype.entries')}}{{Spec2('ES2015')}}Initial definition.
- -

浏览器兼容性

- -

{{Compat("javascript.builtins.Map.entries")}}

- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/foreach/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/foreach/index.html deleted file mode 100644 index 2cf9146f72..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/foreach/index.html +++ /dev/null @@ -1,110 +0,0 @@ ---- -title: Map.prototype.forEach() -slug: Web/JavaScript/Reference/Global_Objects/Map/forEach -translation_of: Web/JavaScript/Reference/Global_Objects/Map/forEach -tags: - - ECMAScript 2015 - - JavaScript - - Map - - Method - - Prototype - - Reference -browser-compat: javascript.builtins.Map.forEach ---- -
{{JSRef}}
- -

forEach() 方法按照插入顺序依次对 Map 中每个键/值对执行一次给定的函数

- -
{{EmbedInteractiveExample("pages/js/map-prototype-foreach.html")}}
- - - -

语法

- -
myMap.forEach(callback([value][,key][,map])[, thisArg])
- -

参数

- -
-
callback
-
-

myMap 中每个元素所要执行的函数。它具有如下的参数

- -
-
value {{Optional_Inline}}
-
每个迭代的值。
-
key {{Optional_Inline}}
-
每个迭代的键。
-
map {{Optional_Inline}}
-
被迭代的map(上文语法框中的 myMap)。
-
-
-
thisArg {{Optional_Inline}}
-
callback 执行中使用的 this 的值。
-
- -

返回值

- -

{{jsxref("undefined")}}.

- -

描述

- -

forEach 方法会对map中每个真实存在的键执行一次给定的 callback 函数。它不会对被删除的键执行函数。然而,它会对每个值为 undefined 的键执行函数。

- -

callback 接收三个参数

- - - -

如果 forEach 中含有 thisArg 参数,那么每次 callback 被调用时,都会被用作 this 的值。否则,undefined 将会被用作 this 的值。按照函数观察到 this 的常用规则callback 函数最终可观察到 this 值。

- -

每个值只被访问一次,除非它被删除了或者在 forEach 结束前被改变了。callback 不会对在被访问前就删除的元素执行。在 forEach 结束前被添加的元素将会被访问。

- -

forEach 会对 Map 对象中的每个元素执行一次 callback。它不会返回值。

- -

示例

- -

输出一个 Map 对象中的内容

- -

以下的代码在每行中打印一个 Map 对象中的元素

- -
function logMapElements(value, key, map) {
-    console.log(`map.get('${key}') = ${value}`)
-}
-new Map([['foo', 3], ['bar', {}], ['baz', undefined]]).forEach(logMapElements)
-// logs:
-// "map.get('foo') = 3"
-// "map.get('bar') = [object Object]"
-// "map.get('baz') = undefined"
-
- -

规范

- - - - - - - - - - - - -
规范
{{SpecName('ESDraft', '#sec-map.prototype.foreach', 'Map.prototype.forEach')}}
- -

浏览器兼容

- - - -

{{Compat("javascript.builtins.Map.forEach")}}

- -

参见

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/get/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/get/index.html deleted file mode 100644 index 6a8a1d771c..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/get/index.html +++ /dev/null @@ -1,82 +0,0 @@ ---- -title: Map.prototype.get() -slug: Web/JavaScript/Reference/Global_Objects/Map/get -translation_of: Web/JavaScript/Reference/Global_Objects/Map/get -tags: - - ECMAScript 2015 - - JavaScript - - Map - - Method - - Prototype - - Reference -browser-compat: javascript.builtins.Map.get ---- -
{{JSRef}}
- -

get() 方法返回某个 Map 对象中的一个指定元素。

- -
{{EmbedInteractiveExample("pages/js/map-prototype-get.html")}}
- - - -

语法

- -
myMap.get(key);
- -

参数

- -
-
key
-
必须参数,也是它唯一的参数,要从目标 Map 对象中获取的元素的键。
-
- -

返回值

- -

返回一个 Map 对象中与指定键相关联的值,如果找不到这个键则返回 undefined

- -

示例

- -

使用 get 方法

- -
var myMap = new Map();
-myMap.set("bar", "foo");
-
-myMap.get("bar");  // 返回 "foo"
-myMap.get("baz");  // 返回 undefined
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('ES2015', '#sec-map.prototype.get', 'Map.prototype.get')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.get', 'Map.prototype.get')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.Map.get")}}

- -

参见

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/has/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/has/index.html deleted file mode 100644 index 05ebad0ea0..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/has/index.html +++ /dev/null @@ -1,122 +0,0 @@ ---- -title: Map.prototype.has() -slug: Web/JavaScript/Reference/Global_Objects/Map/has -translation_of: Web/JavaScript/Reference/Global_Objects/Map/has -tags: - - ECMAScript 2015 - - JavaScript - - Map - - Method - - Prototype - - Reference -browser-compat: javascript.builtins.Map.has ---- -
{{JSRef}}
- -

方法has() 返回一个bool值,用来表明map 中是否存在指定元素.

- -

语法

- -
myMap.has(key);
- -

参数

- -
-
key
-
必填. 用来检测是否存在指定元素的键值.
-
- -

返回值

- -
-
Boolean
-
如果指定元素存在于Map中,则返回true。其他情况返回false
-
- -

案例

- -

使用has方法

- -
var myMap = new Map();
-myMap.set("bar", "foo");
-
-myMap.has("bar");  // returns true
-myMap.has("baz");  // returns false
-
- -

规范列表

- - - - - - - - - - - - - - -
规范名称StatusComment
{{SpecName('ES6', '#sec-map.prototype.has', 'Map.prototype.has')}}{{Spec2('ES6')}}Initial definition.
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
浏览器Chrome谷歌Firefox (Gecko)火狐Internet ExplorerOperaSafari
兼容版本38{{CompatGeckoDesktop("13.0")}}11257.1
-
- -
- - - - - - - - - - - - - - - - - - - - - -
浏览器AndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
兼容版本{{CompatNo}}38{{CompatGeckoMobile("13.0")}}{{CompatNo}}{{CompatNo}}8
-
- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/index.html deleted file mode 100644 index a8afeac9c2..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/index.html +++ /dev/null @@ -1,340 +0,0 @@ ---- -title: Map -slug: Web/JavaScript/Reference/Global_Objects/Map -translation_of: Web/JavaScript/Reference/Global_Objects/Map -tags: - - Class - - ECMAScript 2015 - - JavaScript - - Map - - Reference - - Polyfill -browser-compat: javascript.builtins.Map ---- -
-
{{JSRef}}
-
- -

Map 对象保存键值对,并且能够记住键的原始插入顺序。任何值(对象或者{{Glossary("Primitive", "原始值")}}) 都可以作为一个键或一个值。

- -
-
- -

描述

- -

一个Map对象在迭代时会根据对象中元素的插入顺序来进行 — 一个  {{jsxref("Statements/for...of", "for...of")}} 循环在每次迭代后会返回一个形式为[key,value]的数组。

- -

键的相等(Key equality)

- - - -

Objects 和 maps 的比较

- -

{{jsxref("Object", "Objects")}} 和 Maps 类似的是,它们都允许你按键存取一个值、删除键、检测一个键是否绑定了值。因此(并且也没有其他内建的替代方式了)过去我们一直都把对象当成 Maps 使用。不过 Maps 和 Objects 有一些重要的区别,在下列情况里使用 Map 会是更好的选择:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MapObject
意外的键Map 默认情况不包含任何键。只包含显式插入的键。 -

一个 Object 有一个原型, 原型链上的键名有可能和你自己在对象上的设置的键名产生冲突。

- -
-

注意: 虽然 ES5 开始可以用 Object.create(null) 来创建一个没有原型的对象,但是这种用法不太常见。

-
-
键的类型一个 Map的键可以是任意值,包括函数、对象或任意基本类型。一个Object 的键必须是一个 {{jsxref("String")}} 或是{{jsxref("Symbol")}}。
键的顺序 -

Map 中的 key 是有序的。因此,当迭代的时候,一个 Map 对象以插入的顺序返回键值。

-
-

一个 Object 的键是无序的

- -
-

注意:自ECMAScript 2015规范以来,对象确实保留了字符串和Symbol键的创建顺序; 因此,在只有字符串键的对象上进行迭代将按插入顺序产生键。

-
-
Size Map 的键值对个数可以轻易地通过{{jsxref("Map.prototype.size", "size")}} 属性获取Object 的键值对个数只能手动计算
迭代Mapiterable 的,所以可以直接被迭代。迭代一个Object需要以某种方式获取它的键然后才能迭代。
性能 -

在频繁增删键值对的场景下表现更好。

-
-

在频繁添加和删除键值对的场景下未作出优化。

-
- -

构造函数

- -
-
{{jsxref("Global_Objects/Map/Map", "Map()")}}
-
创建 Map 对象
-
- -

属性

- -
-
Map.length
-
属性 length 的值为 0 。
- 想要计算一个Map 中的条目数量, 使用 {{jsxref("Map.prototype.size")}}.
-
{{jsxref("Map.@@species", "get Map[@@species]")}}
-
本构造函数用于创建派生对象。
-
{{jsxref("Map.prototype")}}
-
表示 Map 构造器的原型。 允许添加属性从而应用于所有的 Map 对象。
-
- -

Map 实例

- -

所有的 Map 对象实例都会继承 {{jsxref("Map.prototype")}}。

- -

属性

- -

{{page('zh-CN/Web/JavaScript/Reference/Global_Objects/Map/prototype','属性')}}

- -

方法

- -

{{page('zh-CN/Web/JavaScript/Reference/Global_Objects/Map/prototype','方法')}}

- -

示例

- -

使用 Map 对象

- -
let myMap = new Map();
-
-let keyObj = {};
-let keyFunc = function() {};
-let keyString = 'a string';
-
-// 添加键
-myMap.set(keyString, "和键'a string'关联的值");
-myMap.set(keyObj, "和键keyObj关联的值");
-myMap.set(keyFunc, "和键keyFunc关联的值");
-
-myMap.size; // 3
-
-// 读取值
-myMap.get(keyString);    // "和键'a string'关联的值"
-myMap.get(keyObj);       // "和键keyObj关联的值"
-myMap.get(keyFunc);      // "和键keyFunc关联的值"
-
-myMap.get('a string');   // "和键'a string'关联的值"
-                         // 因为keyString === 'a string'
-myMap.get({});           // undefined, 因为keyObj !== {}
-myMap.get(function() {}); // undefined, 因为keyFunc !== function () {}
- -

将 NaN 作为 Map 的键

- -

NaN 也可以作为Map对象的键。虽然 NaN 和任何值甚至和自己都不相等(NaN !== NaN 返回true),但下面的例子表明,NaN作为Map的键来说是没有区别的:

- -
let myMap = new Map();
-myMap.set(NaN, "not a number");
-
-myMap.get(NaN); // "not a number"
-
-let otherNaN = Number("foo");
-myMap.get(otherNaN); // "not a number"
-
- -

使用 for..of 方法迭代 Map

- -

Map可以使用for..of循环来实现迭代:

- -
let myMap = new Map();
-myMap.set(0, "zero");
-myMap.set(1, "one");
-for (let [key, value] of myMap) {
-  console.log(key + " = " + value);
-}
-// 将会显示两个log。一个是"0 = zero"另一个是"1 = one"
-
-for (let key of myMap.keys()) {
-  console.log(key);
-}
-// 将会显示两个log。 一个是 "0" 另一个是 "1"
-
-for (let value of myMap.values()) {
-  console.log(value);
-}
-// 将会显示两个log。 一个是 "zero" 另一个是 "one"
-
-for (let [key, value] of myMap.entries()) {
-  console.log(key + " = " + value);
-}
-// 将会显示两个log。 一个是 "0 = zero" 另一个是 "1 = one"
- -

使用 forEach() 方法迭代 Map

- -

Map也可以通过forEach()方法迭代:

- -
myMap.forEach(function(value, key) {
-  console.log(key + " = " + value);
-})
-// 将会显示两个logs。 一个是 "0 = zero" 另一个是 "1 = one"
-
- -

Map 与数组的关系

- -
let kvArray = [["key1", "value1"], ["key2", "value2"]];
-
-// 使用常规的Map构造函数可以将一个二维键值对数组转换成一个Map对象
-let myMap = new Map(kvArray);
-
-myMap.get("key1"); // 返回值为 "value1"
-
-// 使用Array.from函数可以将一个Map对象转换成一个二维键值对数组
-console.log(Array.from(myMap)); // 输出和kvArray相同的数组
-
-// 更简洁的方法来做如上同样的事情,使用展开运算符
-console.log([...myMap]);
-
-// 或者在键或者值的迭代器上使用Array.from,进而得到只含有键或者值的数组
-console.log(Array.from(myMap.keys())); // 输出 ["key1", "key2"]
-
- -

复制或合并 Maps

- -

Map 能像数组一样被复制:

- -
let original = new Map([
-  [1, 'one']
-]);
-
-let clone = new Map(original);
-
-console.log(clone.get(1)); // one
-console.log(original === clone); // false. 浅比较 不为同一个对象的引用
-
- -
-

重要:请记住,数据本身未被克隆。

-
- -

Map对象间可以进行合并,但是会保持键的唯一性。

- -
let first = new Map([
-  [1, 'one'],
-  [2, 'two'],
-  [3, 'three'],
-]);
-
-let second = new Map([
-  [1, 'uno'],
-  [2, 'dos']
-]);
-
-// 合并两个Map对象时,如果有重复的键值,则后面的会覆盖前面的。
-// 展开运算符本质上是将Map对象转换成数组。
-let merged = new Map([...first, ...second]);
-
-console.log(merged.get(1)); // uno
-console.log(merged.get(2)); // dos
-console.log(merged.get(3)); // three
- -

Map对象也能与数组合并:

- -
let first = new Map([
-  [1, 'one'],
-  [2, 'two'],
-  [3, 'three'],
-]);
-
-let second = new Map([
-  [1, 'uno'],
-  [2, 'dos']
-]);
-
-// Map对象同数组进行合并时,如果有重复的键值,则后面的会覆盖前面的。
-let merged = new Map([...first, ...second, [1, 'eins']]);
-
-console.log(merged.get(1)); // eins
-console.log(merged.get(2)); // dos
-console.log(merged.get(3)); // three
- -

使用说明

- -

请注意!为Map设置对象属性也是可以的,但是可能引起大量的混乱。

- -

所以,你还是可以这样做...

- -
let wrongMap = new Map()
-wrongMap['bla'] = 'blaa'
-wrongMap['bla2'] = 'blaaa2'
-
-console.log(wrongMap)  // Map { bla: 'blaa', bla2: 'blaaa2' }
-
- -

...但是,这样做的话,它的行为会不符合预期:

- -
wrongMap.has('bla')    // false
-wrongMap.delete('bla') // false
-console.log(wrongMap)  // Map { bla: 'blaa', bla2: 'blaaa2' }
- -

无论如何,和正确用法比较起来,几乎没有什么不同: 

- -
let myMap = new Map()
-myMap.set('bla','blaa')
-myMap.set('bla2','blaa2')
-console.log(myMap)  // Map { 'bla' => 'blaa', 'bla2' => 'blaa2' }
-
-myMap.has('bla')    // true
-myMap.delete('bla') // true
-console.log(myMap)  // Map { 'bla2' => 'blaa2' }
- -

规范

- - - - - - - - - - - - -
Specification
{{SpecName('ESDraft', '#sec-map-objects', 'Map')}}
- -

浏览器兼容性

- -

{{Compat("javascript.builtins.Map")}}

- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/keys/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/keys/index.html deleted file mode 100644 index 8ded2f526a..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/keys/index.html +++ /dev/null @@ -1,79 +0,0 @@ ---- -title: Map.prototype.keys() -slug: Web/JavaScript/Reference/Global_Objects/Map/keys -translation_of: Web/JavaScript/Reference/Global_Objects/Map/keys -tags: - - ECMAScript 2015 - - Iterator - - JavaScript - - Map - - Method - - Prototype -browser-compat: javascript.builtins.Map.keys ---- -
{{JSRef}}
- -

keys() 返回一个引用的 Iterator 对象。它包含按照顺序插入 Map 对象中每个元素的key值。

- -
{{EmbedInteractiveExample("pages/js/map-prototype-keys.html")}}
- - - -

语法

- -
myMap.keys()
- -

返回值

- -

一个存在引用关系的 {{jsxref("Map")}} iterator 对象.

- -

例子

- -

使用 keys()

- -
var myMap = new Map();
-myMap.set("0", "foo");
-myMap.set(1, "bar");
-myMap.set({}, "baz");
-
-var mapIter = myMap.keys();
-
-console.log(mapIter.next().value); // "0"
-console.log(mapIter.next().value); // 1
-console.log(mapIter.next().value); // Object
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('ES2015', '#sec-map.prototype.keys', 'Map.prototype.keys')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.keys', 'Map.prototype.keys')}}{{Spec2('ESDraft')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.Map.keys")}}

- -

参见

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/map/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/map/index.html deleted file mode 100644 index 9e83f030bc..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/map/index.html +++ /dev/null @@ -1,64 +0,0 @@ ---- -title: Map() 构造函数 -slug: Web/JavaScript/Reference/Global_Objects/Map/Map -translation_of: Web/JavaScript/Reference/Global_Objects/Map/Map -tags: - - Constructor - - JavaScript - - Map - - Reference - - Polyfill -browser-compat: javascript.builtins.Map.Map ---- -
{{JSRef}}
- -

Map() 构造函数 创建 {{jsxref("Map")}} 对象.

- -

语法

- -
new Map([iterable])
- -

参数

- -
-
iterable
-
Iterable 可以是一个{{jsxref("Array", "数组")}}或者其他 iterable 对象,其元素为键值对(两个元素的数组,例如: [[ 1, 'one' ],[ 2, 'two' ]])。 每个键值对都会添加到新的 Map。null 会被当做 undefined。
-
- -

示例

- -
let myMap = new Map([
-  [1, 'one'],
-  [2, 'two'],
-  [3, 'three'],
-])
-
- -

Specifications

- - - - - - - - - - - - -
Specification
{{SpecName('ESDraft', '#sec-map-constructor', 'Map constructor')}}
- -

Browser compatibility

- - - -

{{Compat("javascript.builtins.Map.Map")}}

- -

See also

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/set/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/set/index.html deleted file mode 100644 index ae2ff00036..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/set/index.html +++ /dev/null @@ -1,97 +0,0 @@ ---- -title: Map.prototype.set() -slug: Web/JavaScript/Reference/Global_Objects/Map/set -translation_of: Web/JavaScript/Reference/Global_Objects/Map/set -tags: - - ECMAScript 2015 - - JavaScript - - Map - - Method - - Prototype - - Reference -browser-compat: javascript.builtins.Map.set ---- -

{{JSRef}}

- -

set() 方法为 Map 对象添加或更新一个指定了键(key)和值(value)的(新)键值对。

- -
{{EmbedInteractiveExample("pages/js/map-prototype-set.html")}}
- - - -

语法

- -
myMap.set(key, value);
- -

参数

- -
-
key
-
要添加至相应 Map 对象的元素的键。
-
value
-
要添加至相应 Map 对象的元素的值。
-
- -

返回值

- -

Map 对象

- -

示例

- -

使用 set 方法

- -
var myMap = new Map();
-
-// 将一个新元素添加到 Map 对象
-myMap.set("bar", "foo");
-myMap.set(1, "foobar");
-
-// 在Map对象中更新某个元素的值
-myMap.set("bar", "baz");
-
- -

链式使用 set 方法

- -

因为 Set() 方法返回 Map 对象本身,所以你可以像下面这样链式调用它:

- -
// Add new elements to the map with chaining.
-myMap.set('bar', 'foo')
-     .set(1, 'foobar')
-     .set(2, 'baz');
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('ES2015', '#sec-map.prototype.set', 'Map.prototype.set')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype.set', 'Map.prototype.set')}}{{Spec2('ESDraft')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.Map.set")}}

- -

参见

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/size/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/size/index.html deleted file mode 100644 index 37a0aa484e..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/size/index.html +++ /dev/null @@ -1,78 +0,0 @@ ---- -title: Map.prototype.size -slug: Web/JavaScript/Reference/Global_Objects/Map/size -translation_of: Web/JavaScript/Reference/Global_Objects/Map/size -tags: - - ECMAScript 2015 - - JavaScript - - Map - - Property -browser-compat: javascript.builtins.Map.size ---- -
{{JSRef}}
- -

size 是可访问属性,用于返回 一个{{jsxref("Map")}} 对象的成员数量。

- -

{{EmbedInteractiveExample("pages/js/map-prototype-size.html")}}

- -

这个示例源码保存在GitHub:https://github.com/mdn/interactive-examples。如果你想贡献代码,修改后在GitHub上发推送请求给我们。

- -

 

- -

描述

- -

size 属性的值是一个整数,表示 Map 对象有多少个键值对。size 是只读属性,用set 方法修改size返回 undefined,即不能改变它的值。

- -

示例

- -
var myMap = new Map();
-myMap.set("a", "alpha");
-myMap.set("b", "beta");
-myMap.set("g", "gamma");
-
-myMap.size // 3
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES6', '#sec-get-map.prototype.size', 'Map.prototype.size')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-get-map.prototype.size', 'Map.prototype.size')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容

- -

此页的兼容性表格请查阅:https://github.com/mdn/browser-compat-data 
- 如果你想更新数据,请在GitHub上给我们发推送请求。

- -
-

异常提醒

-
- - - -

相关阅读

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/values/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/values/index.html deleted file mode 100644 index 933b61c0bc..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/values/index.html +++ /dev/null @@ -1,120 +0,0 @@ ---- -title: Map.prototype.values() -slug: Web/JavaScript/Reference/Global_Objects/Map/values -translation_of: Web/JavaScript/Reference/Global_Objects/Map/values -tags: - - ECMAScript 2015 - - Iterator - - JavaScript - - Map - - Method - - Prototype -browser-compat: javascript.builtins.Map.values ---- -
values() 方法返回一个新的Iterator对象。它包含按顺序插入Map对象中每个元素的value值。
- -
 
- -

语法

- -
myMap.values()
- -

返回值

- -

一个新的 Map 可迭代对象.

- -

例子

- -

使用 values()

- -
var myMap = new Map();
-myMap.set("0", "foo");
-myMap.set(1, "bar");
-myMap.set({}, "baz");
-
-var mapIter = myMap.values();
-
-console.log(mapIter.next().value); // "foo"
-console.log(mapIter.next().value); // "bar"
-console.log(mapIter.next().value); // "baz"
- -

规范

- - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('ES6', '#sec-map.prototype.values', 'Map.prototype.values')}}{{Spec2('ES6')}}初始定义
{{SpecName('ESDraft', '#sec-map.prototype.values', 'Map.prototype.values')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support38{{ CompatGeckoDesktop("20") }}{{CompatNo}}257.1
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}38{{ CompatGeckoMobile("20") }}{{CompatNo}}{{CompatNo}}8
-
- -

查看

- - -- cgit v1.2.3-54-g00ecf