Created
February 20, 2020 17:48
-
-
Save ilanl/9cb65f71f6b43f55eaf820f9c435c361 to your computer and use it in GitHub Desktop.
ES6 SetAll Map Override
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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