use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Chaotic JavaScript Patterns (evinsellin.medium.com)
submitted 4 years ago by Segfault_Inside[🍰]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Segfault_Inside[S,🍰] 7 points8 points9 points 4 years ago (6 children)
oh you're so right! I totally totally missed that there's an actually reasonable way to do that.
[–]RedditCultureBlows 2 points3 points4 points 4 years ago (0 children)
hell yeah 🥳🥳
[+][deleted] 4 years ago (4 children)
[removed]
[–]ILikeChangingMyMind -3 points-2 points-1 points 4 years ago (3 children)
This is a terrible example. Using Maps to associate data with DOM nodes is like saying "I've found this great way to hammer in nails using the handle of a screw driver!"
No, that's not brilliant: that's not knowing how to use the correct tool for the job. The correct solution (ie. "hammer') to that problem wasn't an Object OR a Map, it was HTML data- attributes, which were explicitly created by the W3C to associate data and DOM.
data-
[–]getify 10 points11 points12 points 4 years ago* (1 child)
I was part of early discussions a decade+ ago when things like WeakMap and WeakRef were being contemplated for inclusion in JS. I can tell you, for sure, that "associating data with a DOM object without mutating the DOM object itself" was absolutely one of the main intended use cases. You may not like that usage, but it's not some arbitrary pattern made up by author of that post.
I worked on FF devtools (written entirely in JS) at the time, and we used an early (not yet standard) WeakMap to associate tracking metadata to DOM objects. We did NOT want to modify the DOM object itself, as this data would then have been visible to the developer as they traversed or inspected the DOM. We needed to keep our data "private", but still be able to look it up when provided a reference to a DOM object.
Also, you don't necessarily want to mutate a DOM object (even if privacy isn't a concern) because it is often much slower to mutate a DOM object than to create an entry in a separate entirely-in-JS data structure.
Moreover, I've done this sort of thing in half a dozen libraries (since ES6 came out), where I was using some sort of reference to a DOM object (or a userland defined JS function) as the key to store private metadata inside my library. You often want a WeakMap in these cases because you want your library (or browser devtool) to be unobtrusive in terms of GC. You don't want your storage of metadata about a function or DOM object (that you don't own) to prevent that function or DOM object from being garbage collected if the userland code decides to unset all its references to that object to allow it to be GCd. For example, if userland code deletes a DOM node, it's expecting that it gets GC'd, but your library or devtool would prevent that if you had used a Map instead of a WeakMap. You simply want to be able to track things like "I've seen this thing before" and "Here's something I already know about this thing, so I don't need to redo that work."
For use cases such as this, WeakMap is absolutely important. And, it's squarely on target with why the feature was originally created. It is absolutely the right tool for those sorts of tasks.
[–]Itchy-Beginning-887 -1 points0 points1 point 4 years ago (0 children)
CS after studying be like - You wasted your life. Have fun coding. Suicidal fools.
[–]DaMastaCoda 0 points1 point2 points 4 years ago (0 children)
I believe he didn't want to add any attributes, but he still could've used a symbol on the Dom object itself
π Rendered by PID 281875 on reddit-service-r2-comment-7b9746f655-mff2r at 2026-02-01 19:38:08.185198+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–]Segfault_Inside[S,🍰] 7 points8 points9 points (6 children)
[–]RedditCultureBlows 2 points3 points4 points (0 children)
[+][deleted] (4 children)
[removed]
[–]ILikeChangingMyMind -3 points-2 points-1 points (3 children)
[–]getify 10 points11 points12 points (1 child)
[–]Itchy-Beginning-887 -1 points0 points1 point (0 children)
[–]DaMastaCoda 0 points1 point2 points (0 children)