Skip to content

Instantly share code, notes, and snippets.

@akopcz2
Last active September 12, 2020 21:22
Show Gist options
  • Save akopcz2/e0d2bbef98e63137ab6752e8c76b8fb4 to your computer and use it in GitHub Desktop.
Save akopcz2/e0d2bbef98e63137ab6752e8c76b8fb4 to your computer and use it in GitHub Desktop.
DOMStore FB Interview Question
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