WeakSet() 构造函数
WeakSet()
构造函数创建 WeakSet
对象。
语法
参数
示例
使用 WeakSet 对象
js
const ws = new WeakSet();
const foo = {};
const bar = {};
ws.add(foo);
ws.add(bar);
ws.has(foo); // true
ws.has(bar); // true
ws.delete(foo); // 将 foo 从集合中移除
ws.has(foo); // false,foo 已经被移除
ws.has(bar); // true,bar 被保留
请注意 foo !== bar
。它们是相似的对象,但它们不是同一个对象。因此,它们都会被添加到集合中。
规范
Specification |
---|
ECMAScript Language Specification # sec-weakset-constructor |
浏览器兼容性
BCD tables only load in the browser