Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
1–10 of 30 posts
Re: Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
#2Re: Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
#3Re: Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
#4This might be one of the more interesting articles I've read in a few years that deal with low level direct DOM manipulation vs "yet another thing about React".
I was recently refactoring some code and wondering whether to use a (JS) class… The search results were mostly about React Class Components. And I was contemplating building a reusable component… results from the past decade were split between React Components and Web components. (In retrospect I should’ve tried DDG or something not connected to my search history lol).
Anyway where are the vanilla JS/TS DOM manipulators blogging these days? I guess here is one, thankful for the post :)
Re: Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
#5 node.querySelectorAll('tr[data-selected="true"]');
In other words.. "use the DOM to handle the DOM."Re: Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
#6Re: Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
#7I think MDN has a good comparison: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
The only real downside of maps is that they don't support JSON serialization. However you can fix this pretty easily by using a map with an overridden `toJSON` for serializable keys.
class StringMap extends Map {
toJSON() {
return Object.fromEntries(this);
}
}Re: Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
#8Why not just use the 'dataset' property of the element itself? Then you can use querySelectorAll to find your selected rows automatically: node.querySelectorAll('tr[data-selected="true"]'); In other words.. "use the DOM to handle the DOM."
Re: Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
#9I would argue that all "mapping" structures should use Map these days. Objects should be used only for record/struct data with a fixed set of programmer-named keys. I think MDN has a good comparison: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... The only real downside of maps is that they don't support JSON serialization. However you can fix this pretty easily by using a map with an overridden `toJS…
Re: Why I Like Using Maps (and WeakMaps) for Handling DOM Nodes
#10I would argue that all "mapping" structures should use Map these days. Objects should be used only for record/struct data with a fixed set of programmer-named keys. I think MDN has a good comparison: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... The only real downside of maps is that they don't support JSON serialization. However you can fix this pretty easily by using a map with an overridden `toJS…
I'd say the big downside is that they don't support the general looking accessor that I'd wager way way way too many people will accidentally use. Myself definitely being one of them. Would have been better if they didn't "seem" to work with the wrong syntax, at least. But, alas, here we are.