尝试一下
const set1 = new Set();
set1.add(42);
set1.add(42);
set1.add(13);
for (const item of set1) {
console.log(item);
// Expected output: 42
// Expected output: 13
}
语法
js
add(value)
参数
value-
要添加到
Set对象的元素的值。
返回值
添加了值的 Set 对象。
示例
>使用 add() 方法
js
const mySet = new Set();
mySet.add(1);
mySet.add(5).add("some text"); // 可以链式调用
console.log(mySet);
// Set [1, 5, "some text"]
规范
| 规范 |
|---|
| ECMAScript® 2027 Language Specification> # sec-set.prototype.add> |