此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
Set.prototype.clear()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Set 实例的clear() 方法移除该集合中的所有元素。
In this article
尝试一下
const set1 = new Set();set1.add(1);set1.add("foo");console.log(set1.size);// Expected output: 2set1.clear();console.log(set1.size);// Expected output: 0语法
js
clear()参数
无。
返回值
无(undefined)。
示例
>使用 clear() 方法
js
const mySet = new Set();mySet.add(1);mySet.add("foo");console.log(mySet.size); // 2console.log(mySet.has("foo")); // truemySet.clear();console.log(mySet.size); // 0console.log(mySet.has("foo")); // false规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-set.prototype.clear> |