You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 7<br />
The keys() and values() iterators both return a list of the unique values in the Set.<br />
The entries() iterator yields a list of entry arrays, where both items of the array are<br />
the unique Set values. The default iterator for a Set is its values() iterator.<br />
Symbols<br />
ES6 introduces a new data type called Symbol. A Symbol is guaranteed to be unique<br />
and immutable. Symbols are usually used as an identifier for object properties.<br />
They can be considered as uniquely generated IDs. You can create Symbols with the<br />
Symbol() factory method—remember that this is not a constructor and hence you<br />
should not use a new operator:<br />
let s = Symbol();<br />
console.log(typeof s); //symbol<br />
Unlike strings, Symbols are guaranteed to be unique and hence help in preventing<br />
name clashes. With Symbols, we have an extensibility mechanism that works for<br />
everyone. ES6 comes with a number of predefined built-in Symbols that expose<br />
various meta behaviors on <strong>JavaScript</strong> object values.<br />
Iterators<br />
Iterators have been around in other programming languages for quite some time.<br />
They give convenience methods to work with collections of data. ES6 introduces<br />
iterators for the same use case. ES6 iterators are objects with a specific interface.<br />
Iterators have a next() method that returns an object. The returning object has two<br />
properties—value (the next value) and done (indicates whether the last result has<br />
been reached). ES6 also defines an Iterable interface, which describes objects that<br />
must be able to produce iterators. Let's look at an array, which is an iterable, and the<br />
iterator that it can produce to consume its values:<br />
var a = [1,2];<br />
var i = a[Symbol.iterator]();<br />
console.log(i.next()); // { value: 1, done: false }<br />
console.log(i.next()); // { value: 2, done: false }<br />
console.log(i.next()); // { value: undefined, done: true }<br />
As you can see, we are accessing the array's iterator via Symbol.iterator() and<br />
calling the next() method on it to get each successive element. Both value and done<br />
are returned by the next() method call. When you call next() past the last element<br />
in the array, you get an undefined value and done: true, indicating that you have<br />
iterated over the entire array.<br />
[ 175 ]<br />
www.it-ebooks.info