From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/global_objects/map/set/index.html | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/set/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/map/set') 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 new file mode 100644 index 0000000000..8184278bc3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/set/index.html @@ -0,0 +1,96 @@ +--- +title: Map.prototype.set() +slug: Web/JavaScript/Reference/Global_Objects/Map/set +tags: + - ECMAScript6 + - JavaScript + - Map + - Method + - Prototype + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/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")}}

+ +

参见

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