WeakMap.prototype.set()
WeakMap 实例的 set() 方法向此 WeakMap 添加具有指定键和值的新元素。
¥The set() method of WeakMap instances adds a new element with a specified key
and value to this WeakMap.
Try it
语法
参数
返回值
例外情况
示例
使用 set() 方法
¥Using the set() method
js
const wm = new WeakMap();
const 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");
// Using a non-registered symbol as key
const sym = Symbol("foo");
wm.set(sym, "baz");
wm.set(Symbol.iterator, "qux");
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-weakmap.prototype.set |
浏览器兼容性
BCD tables only load in the browser