-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweakmap-muse
More file actions
32 lines (17 loc) · 889 Bytes
/
weakmap-muse
File metadata and controls
32 lines (17 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
console.clear();
var map = new WeakMap();
var a = document.createElement('div');
map.set(a,()=>'hello');
console.log(map.get(a)());
//:
var mem = [];
mem.rnd = ()=>Math.random();
mem.add = (node,fn,index)=>(index=mem.rnd(),mem.push({index,node,fn}),index);
mem.rem = node=>(i=mem.findIndex(o=>o.node===node)) ?? mem.splice(i,1,0);
mem.get = node=>mem.find(o=>o.index==node.dataset.index);
var div = document.createElement('div');
div.dataset.index = mem.add(div,()=>'world');
console.log(mem.get(div).fn());
mem.rem(div);
//
mem.rem = (node,i)=>(i=mem.findIndex(o=>o.node===node),mem.splice(i,1));