Set.prototype.clear()

Set 实例的 clear() 方法移除该集合中的所有元素。

尝试一下

语法

js
clear()

参数

无。

返回值

无(undefined)。

示例

使用 clear() 方法

js
const mySet = new Set();
mySet.add(1);
mySet.add("foo");

console.log(mySet.size); // 2
console.log(mySet.has("foo")); // true

mySet.clear();

console.log(mySet.size); // 0
console.log(mySet.has("foo")); // false

规范

Specification
ECMAScript Language Specification
# sec-set.prototype.clear

浏览器兼容性

BCD tables only load in the browser

参见