Skip to content

Instantly share code, notes, and snippets.

@ilanl
Created February 20, 2020 17:48
Show Gist options
  • Save ilanl/9cb65f71f6b43f55eaf820f9c435c361 to your computer and use it in GitHub Desktop.
Save ilanl/9cb65f71f6b43f55eaf820f9c435c361 to your computer and use it in GitHub Desktop.
ES6 SetAll Map Override
class MyMap extends Map {
constructor() {
super();
this.counter = 0;
this.all_value = null;
}
set(key, value) {
if (value) {
super.set(key, { ts: this.counter++, v: value });
} else {
super.set(key, null);
}
}
setAll(value) {
this.all_value = { ts: this.counter++, v: value };
}
get(key) {
let val;
if (
super.get(key) &&
this.all_value &&
this.all_value.ts >= super.get(key).ts
) {
val = this.all_value;
} else {
val = super.get(key);
}
val = val ? val.v : undefined;
console.log(val);
return val;
}
}
let map = new MyMap();
console.log("start");
map.set("a", 1);
map.set("b", 2);
map.setAll(-1);
map.set("c", 3);
map.set("b", 4);
map.get("a");
map.get("b");
map.get("c");
map.get("d");
console.log("end");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment