Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Using DOM Nodes As Keys

<span id="thing" class="thing">a thing.</span>

<script>
const myWeakMap = new WeakMap();

// Set a value to a specific node reference.
myWeakMap.set(document.getElementById('thing'), 'some value');

// Access that value by passing the same reference.
console.log(myWeakMap.get(document.querySelector('.thing')); // 'some value'
</script>
Source by macarthur.me #
 
PREVIOUS NEXT
Tagged: #Using #DOM #Nodes #As #Keys
ADD COMMENT
Topic
Name
7+9 =