How to link javascript objects to DOM nodes?

Can I, and if so, how can I link javascript objects to DOM nodes? My requirements:

  • real objects, not just JSON-stringified attributes or so
  • do not leak memory
  • should also work in non-standard browsers like IE8 or so

I thought about this with a global array that stores data and puts indexes in the attributes of the nodes, but this will be a memory leak, because there is still a link from the array to the data when the nodes are not in the th HOUSE. This is for a web application, so this can be a problem.

+3
source share
3 answers

WeakMap, , ( 36, 12, IE 11, firefox 36, 7.1):

let map = new WeakMap();
let node = document.getElementById("unicorn");
let data = {};

window.map.set(node, data);

, WeakMap .

+1

, jQuery data, . , .

+4

Since tags do not mention jquery. Submitting a response using Mootools.

http://mootools.net/docs/core/Element/Element#Element:store

0
source

Source: https://habr.com/ru/post/1780852/


All Articles