WeakMap.prototype.set()

set() 方法根据指定的keyvalue在 WeakMap对象中添加新/更新元素。

语法

wm.set(key, value);

参数

key
必须的。必须是对象。是要在WeakMap 对象中添加元素的key部分。
value
必须的。任意的值。是要在WeakMap 对象中添加/元素的value部分。

返回值

WeakMap对象

例子

使用set方法

var wm = new WeakMap();
var obj = {};

// Add new elements to the WeakMap
wm.set(obj, "foo").set(window, "bar"); // chainable

// Update an element in the WeakMap
wm.set(obj, "baz");

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
WeakMap.prototype.set
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
WeakMap.prototype.set
Draft

浏览器兼容

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
setChrome Full support 36Edge Full support 12Firefox Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
IE Partial support 11
Notes
Partial support 11
Notes
Notes Returns 'undefined' instead of the 'Map' object.
Opera Full support 23Safari Full support 8WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
Opera Android Full support 24Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
Partial support  
Partial support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

Firefox-特殊说明

  • 直到Firefox 33 (Firefox 33 / Thunderbird 33 / SeaMonkey 2.30), WeakMap.prototype.set returned undefined 而不支持联式。它已经修复 (bug 1031632). 这样的实现也存在于 Chrome/v8 (issue).

See also