set()
方法根据指定的key
和value在
WeakMap
对象中添加新/更新元素。
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.
语法
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 |
浏览器兼容
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out
https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
set |
Chrome Full support 36 | Edge Full support 12 | Firefox Full support 6
|
IE Partial support 11
|
Opera Full support 23 | Safari Full support 8 | WebView Android Full support 37 | Chrome Android Full support 36 | Firefox Android Full support 6
|
Opera Android Full support 24 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12
|
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
returnedundefined
而不支持联式。它已经修复 (bug 1031632). 这样的实现也存在于 Chrome/v8 (issue).