WeakSet.prototype.has()
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
实例的 has()
方法返回一个布尔值,指示该 WeakSet
中指定的对象是否存在。
尝试一下
const weakset1 = new WeakSet();
const object1 = {};
const object2 = {};
weakset1.add(object1);
console.log(weakset1.has(object1));
// Expected output: true
console.log(weakset1.has(object2));
// Expected output: false
语法
js
has(value)
参数
value
-
要测试是否在该
WeakSet
对象中存在的值。
返回值
如果指定的值存在于 WeakSet
对象中,则返回 true
;否则返回 false
。如果 value
不是对象或非全局注册的符号,则始终返回 false
。
示例
使用 has()
方法
js
const ws = new WeakSet();
const obj = {};
ws.add(window);
ws.has(window); // 返回 true
ws.has(obj); // 返回 false
// 存储非全局注册的符号
const sym = Symbol("foo");
ws.add(sym);
ws.add(Symbol.iterator);
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-weakset.prototype.has |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
has |
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.