Skip to content

Commit 7ef920b

Browse files
committed
Add Set
1 parent e773d5a commit 7ef920b

1 file changed

Lines changed: 222 additions & 0 deletions

File tree

content/JavaScript/set.jsdoc

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
Set : Object
2+
3+
Sets are a collection of %%/Object|Objects%% where each
4+
object can only appear once in the set.
5+
6+
Iterable:
7+
true
8+
9+
Version:
10+
ECMAScript 6
11+
12+
----
13+
Set() : Set
14+
15+
Same as %%#new_Set|**new Set()**%%.
16+
17+
----
18+
Set(iterable : Object) : Set
19+
20+
Same as %%#new_Set_Object|**new Set(iterable)**%%.
21+
22+
----
23+
new Set() : Set
24+
25+
Creates an empty Set.
26+
27+
<example>
28+
var x = new Set();
29+
x.add(1);
30+
x.add('a');
31+
x.add({ foo: 'bar' });
32+
33+
for (var item of x) {
34+
console.dir(item);
35+
}
36+
</example>
37+
38+
----
39+
new Set(iterable : Object) : Set
40+
41+
Creates a Set by iterating over **iterable** and adding
42+
each value to the Set.
43+
44+
<example>
45+
var fromArray = new Set([1, '1', { foo: 'bar' }]);
46+
47+
for (var item of fromArray) {
48+
console.dir(item);
49+
}
50+
51+
var generator = function*() {
52+
yield 3;
53+
yield 'javascript';
54+
};
55+
56+
var fromGenerator = new Set(generator());
57+
58+
for (var item of fromGenerator) {
59+
console.dir(item);
60+
}
61+
</example>
62+
63+
----
64+
instance.size : Number
65+
66+
The number of value in **this**.
67+
68+
<example>
69+
var x = new Set(['a', 'b', 'c']);
70+
71+
console.log(x.size);
72+
73+
x.add('d');
74+
75+
console.log(x.size);
76+
77+
x.clear();
78+
79+
console.log(x.size);
80+
</example>
81+
82+
----
83+
prototype.add(value : Object) : Set
84+
85+
Stores **value** in **this**. If **value** is already stored,
86+
there is no change to the Set.
87+
Returns **this**.
88+
89+
<example>
90+
var x = new Set(['a', 'b', 'c']);
91+
92+
console.log(x.has('a'));
93+
console.log(x.has('d'));
94+
95+
x.add('d');
96+
console.log(x.has('d'));
97+
</example>
98+
99+
----
100+
prototype.clear() : undefined
101+
102+
Clears all values from **this**.
103+
104+
<example>
105+
var x = new Set(['a', 'b', 'c']);
106+
107+
console.log(x.size);
108+
109+
x.clear();
110+
111+
console.log(x.size);
112+
</example>
113+
114+
----
115+
prototype.delete(value : Object) : Boolean
116+
117+
Removes **value* from **this**.
118+
Returns **true** if **value** was in **this** before deleting it.
119+
120+
<example>
121+
var x = new Set(['a', 'b', 'c']);
122+
123+
console.log('size before delete: ' + x.size);
124+
console.log('deleted "a" = ' + x.delete('a'));
125+
126+
console.log('size after delete: ' + x.size);
127+
console.log('deleted "foo" = ' + x.delete('foo'));
128+
</example>
129+
130+
----
131+
prototype.entries() : Iterator
132+
133+
Returns an iterator of the items in **this** where the
134+
%%Iterator#next|**values**s%% of the iterator are of the form
135+
**[value : %%/Object|Object%%, value : %%/Object|Object%%]**
136+
(**value** is duplicated twice).
137+
138+
See also %%#keys|**keys()**%% and %%#values|**values()**%%.
139+
140+
<example>
141+
var x = new Set(['a', 'b', 'c']);
142+
143+
// Use for (... of ...) to loop over the iterator
144+
for (var entry of x.entries()) {
145+
console.dir(entry);
146+
}
147+
148+
// Or access the iterator manually
149+
var entries = x.entries();
150+
151+
console.dir(entries.next());
152+
console.dir(entries.next());
153+
console.dir(entries.next());
154+
console.dir(entries.next());
155+
</example>
156+
157+
----
158+
prototype.forEach(callback(value : Object, key : Object, set : Set) : undefined, [thisArg : Object]) : undefined
159+
160+
Calls **callback** for each value in **this**.
161+
162+
The **Set** passed to **callback** is the **this** of the call to **forEach**.
163+
164+
<example>
165+
var x = new Set(['a', 'b', 'c']);
166+
167+
x.forEach(function(value) {
168+
console.dir(value);
169+
});
170+
</example>
171+
172+
----
173+
prototype.has(value : Object) : Boolean
174+
175+
Returns **true** if the set contains **value**.
176+
177+
<example>
178+
var x = new Set(['a', 'b', 'c']);
179+
180+
console.log(x.has('a'));
181+
console.log(x.has('foo'));
182+
</example>
183+
184+
----
185+
prototype.keys() : Iterator
186+
187+
Same as %%#values|values()%%.
188+
189+
----
190+
prototype.values() : Iterator
191+
192+
Returns an iterator of the values in **this**.
193+
194+
The **values** function is also returned for
195+
**this[%%/Symbol#iterator|Symbol.iterator%%]** so you can
196+
iterate over **this** directly to get the values.
197+
198+
See also %%#entries|**entries()**%% and %%#keys|**keys()**%%.
199+
200+
<example>
201+
var x = new Set(['a', 'b', 'c']);
202+
203+
// values is returned by x[Symbol.iterator] so you can just
204+
// use x directly in the for (... of ...) loop
205+
for (var value of x) {
206+
console.dir(value);
207+
}
208+
209+
// You can iterate over values explicitly
210+
for (var value of x.values()) {
211+
console.dir(value);
212+
}
213+
214+
// Or access the iterator manually
215+
var values = x.values();
216+
217+
console.dir(values.next());
218+
console.dir(values.next());
219+
console.dir(values.next());
220+
console.dir(values.next());
221+
</example>
222+

0 commit comments

Comments
 (0)