add()
方法用来向一个 Set
对象的末尾添加一个指定的值。
mySet.add(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 |
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 |
Set.prototype.add
会返回undefined 并且不可链式调用。
该问题(bug 1031632)已修复。Chrome/v8 中也有该问题 (issue).