frontend-challenges
frontend-challenges copied to clipboard
483 - Node Store - javascript
index.js
/**
* A store that uses DOM elements as keys.
* Cannot use ES6 Map, must implement custom solution.
*/
class NodeStore {
constructor() {
this.store = {};
}
/**
* Set a value for the given DOM node
* @param {Node} node - DOM node
* @param {any} value - value to store
*/
set(node, value) {
this.store[Symbol.for(node)] = value;
}
/**
* Get the value for the given DOM node
* @param {Node} node - DOM node
* @returns {any} stored value or undefined
*/
get(node) {
return this.store[Symbol.for(node)];
}
/**
* Check if the given DOM node exists as a key
* @param {Node} node - DOM node
* @returns {boolean} true if node exists
*/
has(node) {
return Symbol.for(node) in this.store;
}
}
export { NodeStore };