var data = {};
var element = document.getElementById("myDiv");
data[element] = metadata;
data["[Object HTMLDivElement]"] // metadata
var m = new Map();
var o = {p: "Hello World"};
m.set(o,"content")
m.get(o) "content"
m.has(o) true
m.delete(o) true
m.has(o) false
var map = new Map([["name","张三"],["title","Author"]]);
map.size 2
map.has("name") true
map.get("name") "张三"
map.has("title") true
map.get("title") "Author"
Map();
map.set(['a'],555);
map.get(['a']) undefined
Map();
var k1 = ['a'];
var k2 = ['a'];
map
.set(k1,111)
.set(k2,222);
map.get(k1) 111
map.get(k2) 222
let map = Map();
map.set(NaN,123);
map.get(NaN) 123
map.set(-0,123);
map.get(+0) 123
等同于使用map.entries()
for (let [key,value] of map) {
console.log(key,value);
}
map[Symbol.iterator] === map.entries
true
WeakMap()
map.set(1,2)
TypeError: 1 is not an object!
map.set(Symbol(),1)"> TypeError: Invalid value used as weak map key
var wm = WeakMap();
var element = document.querySelector(".element");
wm.set(element,"Original");
wm.get(element) "Original"
element.parentNode.removeChild(element);
element = null;
wm.get(element) undefined
let _counter = WeakMap();
let _action = WeakMap();
class Countdown {
constructor(counter,action) {
_counter.set(this,counter);
_action.set();
if (counter < 1) return;
counter--;
_counter.set(if (counter === 0) {
_action.get()();
}
}
}
let c = new Countdown(2,() => console.log('DONE'));
c.dec()
c.dec()
DONE