WeakSet() 构造函数
语法
参数
示例
使用 WeakSet 对象
¥Using the WeakSet object
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); // removes foo from the set
ws.has(foo); // false, foo has been removed
ws.has(bar); // true, bar is retained
请注意,foo !== bar
。虽然它们是相似的对象,但它们不是同一个对象。因此它们都被添加到集合中。
¥Note that foo !== bar
. While they are similar objects, they are not
the same object. And so they are both added to the set.
规范
Specification |
---|
ECMAScript Language Specification # sec-weakset-constructor |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also