add() 方法用来向一个 Set 对象的末尾添加一个指定的值。
语法
mySet.add(value);
参数
- value
-
必需。需要添加到
Set对象的元素的值。
返回值
Set 对象本身
注意:不能添加重复的值
示例
var mySet = new Set();
mySet.add(1);
mySet.add(5).add("some text"); // 可以链式调用
console.log(mySet);
// Set [1, 5, "some text"]
mySet.add(5).add(1);
console.log(mySet);
//Set [1, 5] // 重复的值没有被添加进去
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Set.prototype.add |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) Set.prototype.add |
Draft |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains.
Find out how you can help!
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 38 | 13.0 (13.0) | 11 | 25 | 7.1 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | 未实现 | 38 | 13.0 (13.0) | 未实现 | 未实现 | 8 |
火狐备注
- 低于 Firefox 33 (Firefox 33 / Thunderbird 33 / SeaMonkey 2.30)的版本,
Set.prototype.add会返回undefined 并且不可链式调用。该问题(bug 1031632)已修复。Chrome/v8 中也有该问题 (issue).