frontend-challenges icon indicating copy to clipboard operation
frontend-challenges copied to clipboard

483 - Node Store - javascript

Open jsartisan opened this issue 2 months ago • 0 comments

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 };

jsartisan avatar Sep 27 '25 05:09 jsartisan