06.07.2017 Views

Mastering JavaScript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3<br />

Maps<br />

ECMAScript 6 introduces maps. A map is a simple key-value map and can iterate its<br />

elements in the order of their insertion. The following snippet shows some methods<br />

of the Map type and their usage:<br />

var founders = new Map();<br />

founders.set("facebook", "mark");<br />

founders.set("google", "larry");<br />

founders.size; // 2<br />

founders.get("twitter"); // undefined<br />

founders.has("yahoo"); // false<br />

for (var [key, value] of founders) {<br />

console.log(key + " founded by " + value);<br />

}<br />

// "facebook founded by mark"<br />

// "google founded by larry"<br />

Sets<br />

ECMAScript 6 introduces sets. Sets are collections of values and can be iterated in the<br />

order of the insertion of their elements. An important characteristic about sets is that<br />

a value can occur only once in a set.<br />

The following snippet shows some basic operations on sets:<br />

var mySet = new Set();<br />

mySet.add(1);<br />

mySet.add("Howdy");<br />

mySet.add("foo");<br />

mySet.has(1); // true<br />

mySet.delete("foo");<br />

mySet.size; // 2<br />

for (let item of mySet) console.log(item);<br />

// 1<br />

// "Howdy"<br />

[ 95 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!