Set.prototype.add()

如果该集合中不存在具有相同值的元素,则 Set 实例的 add() 方法将具有指定值的新元素插入到该集合中

¥The add() method of Set instances inserts a new element with a specified value in to this set, if there isn't an element with the same value already in this set

Try it

语法

¥Syntax

js
add(value)

参数

¥Parameters

value

要添加到 Set 对象的元素的值。

返回值

¥Return value

具有附加值的 Set 对象。

¥The Set object with added value.

示例

¥Examples

使用 add() 方法

¥Using the add() method

js
const mySet = new Set();

mySet.add(1);
mySet.add(5).add("some text"); // chainable

console.log(mySet);
// Set [1, 5, "some text"]

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看