WeakSet.prototype.delete()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
WeakSet
实例的 delete()
方法从该 WeakSet
对象中删除指定的元素。
尝试一下
const weakset1 = new WeakSet();
const object1 = {};
weakset1.add(object1);
console.log(weakset1.has(object1));
// Expected output: true
weakset1.delete(object1);
console.log(weakset1.has(object1));
// Expected output: false
语法
js
weakSetInstance.delete(value)
参数
value
-
要从
WeakSet
对象中删除的值。
返回值
如果成功在 WeakSet
对象中删除了元素,则返回 true
。如果未在 WeakSet
中找到该 value
,则返回 false
。如果 value
不是对象或非全局注册的符号,则始终返回 false
。
示例
使用 delete() 方法
js
const ws = new WeakSet();
const obj = {};
ws.add(window);
ws.delete(obj); // 返回 false。没有找到要删除的对象。
ws.delete(window); // 返回 true。成功删除。
ws.has(window); // 返回 false。WeakMap 中已经不存在 window 对象。
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-weakset.prototype.delete |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
delete |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
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.