首页 > 代码库 > [ES6] 18. Map

[ES6] 18. Map

ES6 provides Map, it is a set of k-v pair. Key can be number, string, object, function and even undefined.

var m = new Map();

 

Methods:


 

1. set(k,v)

m.set("edition", 6)        // 键是字符串m.set(262, "standard")     // 键是数值m.set(undefined, "nah")    // 键是undefinedvar hello = function() {console.log("hello");}m.set(hello, "Hello ES6!") // 键是函数

 

2. get(k)

m.get(hello)  // Hello ES6!m.get("edition")  // 6

 

3. has(k)

m.has("edition")     // truem.has("years")       // falsem.has(262)           // truem.has(undefined)     // truem.has(hello)         // true

 

4. delete(k)

m.delete(undefined)m.has(undefined)       // false

 

5. clear()

6. size()

m.size(); // 3m.clear(); m.size(); // 0

 

[ES6] 18. Map