Set.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 July 2015.
Set
实例的 has()
方法返回一个布尔值来指示对应的值是否存在于该集合中。
尝试一下
语法
js
has(value)
参数
value
-
要测试是否存在于
Set
对象中的值。
返回值
如果 Set
对象中存在具有指定值的元素,则返回 true
;否则返回 false
。
示例
使用 has() 方法
js
const mySet = new Set();
mySet.add("foo");
console.log(mySet.has("foo")); // true
console.log(mySet.has("bar")); // false
const set1 = new Set();
const obj1 = { key1: 1 };
set1.add(obj1);
console.log(set1.has(obj1)); // true
console.log(set1.has({ key1: 1 })); // false, 因为它们是不同的对象引用
console.log(set1.add({ key1: 1 })); // 现在 set1 包含 2 个条目
规范
Specification |
---|
ECMAScript Language Specification # sec-set.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.