Last active
September 12, 2020 21:22
-
-
Save akopcz2/e0d2bbef98e63137ab6752e8c76b8fb4 to your computer and use it in GitHub Desktop.
DOMStore FB Interview Question
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
We need to use a WeakMap because if we store the actual dom node as on Object or Map we would run into this issue [object Object] | |
class DOMStore { | |
constructor(){ | |
this.store = new WeakMap(); | |
} | |
set(key,val){ | |
this.store.set(key,val); | |
} | |
get(key){ | |
return this.store.get(key); | |
} | |
has(key){ | |
return this.store.has(key); | |
} | |
} | |
const store = new DOMStore(); | |
// From querySelector, an event listener, etc... | |
const a = document.createElement('div'); | |
const b = document.createElement('p'); | |
store.set(a, 3); | |
store.set(b, 'x'); | |
store.get(a) // 3 | |
store.get(b) // 'x' | |
store.has(a) // true | |
store.has(b) // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment