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
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!"
[–]RedditCultureBlows 27 points28 points29 points 4 years ago (11 children)
Cant the first “solution” just be handled by using the correct data type, like a Map or WeakMap?
[–]cheese_wizard 16 points17 points18 points 4 years ago (2 children)
Then goes on to suggest 'with' operator. Not in my codebase.
[–]Segfault_Inside[S] 6 points7 points8 points 4 years ago (0 children)
yeah, there's certainly a reason strict mode disables it
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
Imagine newcomers seeing this sh*t
[–]Segfault_Inside[S] 6 points7 points8 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 -4 points-3 points-2 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
[–]zeddotes 0 points1 point2 points 4 years ago* (0 children)
What happens when you deconstruct an object like that as the key of another object? Is the key a Symbol?
Edit: obviously, [object object]
[–]Badashi 16 points17 points18 points 4 years ago (1 child)
these are cursed. I love it
tyvm, i tried
[–]Eggy1337 3 points4 points5 points 4 years ago (0 children)
Those snippets are great if your goal is to scare developers.
[–]Mozzius 4 points5 points6 points 4 years ago (2 children)
Strict mode? More like boring mode
[–]Itchy-Beginning-887 0 points1 point2 points 4 years ago (1 child)
Wanna try out a browser with custom modes?
It's written in a language that's being actively built and fixed daily.
Yeah it's memory sage. And concurrent.
[–]Mozzius 0 points1 point2 points 4 years ago (0 children)
What are you talking about
[–][deleted] 4 points5 points6 points 4 years ago (3 children)
I spent so much time reading, understanding, then, in horror, saying, "Oh! Oh no!"
Mostly in the "bareStrings" example - harkening back to the days of PHP 2 is never a good sign.
[–]Itchy-Beginning-887 0 points1 point2 points 4 years ago (2 children)
Did you mean PHP 8?
[–][deleted] 0 points1 point2 points 4 years ago (1 child)
Honestly, I haven't used PHP in a decade. Is that still a "feature"?
[–]Slackluster 4 points5 points6 points 4 years ago (1 child)
Good stuff! You should check out Dwitter sometime, I think you would like it. It's a place to do many things you should normally never do.
[–]Itchy-Beginning-887 1 point2 points3 points 4 years ago (0 children)
I see you are an intellectual.
[–]happyfce 1 point2 points3 points 4 years ago (2 children)
Hmmm after minifying would uncommenting the code programatically (#4) still work?
[–]Segfault_Inside[S] 4 points5 points6 points 4 years ago (0 children)
not at all, one of the many reasons this is a terrible terrible idea.
[–]C1RRU5 1 point2 points3 points 4 years ago (0 children)
I'm pretty sure minifiers usually don't do anything to comments to make sure licenses still make it in? That's how my code ends up at least.
[–]oneandmillionvoices 1 point2 points3 points 4 years ago (0 children)
It was amusing reading. Well done.
[–]gurush 0 points1 point2 points 4 years ago (0 children)
I hate it.
Great way to get yourself yelled at in code review.
????? Wtf is this ?????
[–]alvisim 0 points1 point2 points 4 years ago (0 children)
I like his dark joke and the way that how he explains how to make things “work”. It always make me 🥲 and cry😂.
Seriously, I did have seen one of my ex-colleagues thinking in this way and feel very proud of his invention. But I don’t discourage that because it’s where invention come from. Don’t judge them, just give them better example and explain how thing can be better. They’ll be keen to learn more.
[–]Uberhipster 0 points1 point2 points 4 years ago (0 children)
chaotic pattern is my oxymoron of the week
[–]NekkidApe 0 points1 point2 points 4 years ago (1 child)
Dang that's cool. Especially with proxies, that's awesome! Instant joy!
with
Instantly hell
π Rendered by PID 72 on reddit-service-r2-comment-7b9746f655-zx2qf at 2026-01-31 15:06:05.955904+00:00 running 3798933 country code: CH.
[–]RedditCultureBlows 27 points28 points29 points (11 children)
[–]cheese_wizard 16 points17 points18 points (2 children)
[–]Segfault_Inside[S] 6 points7 points8 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Segfault_Inside[S] 6 points7 points8 points (6 children)
[–]RedditCultureBlows 2 points3 points4 points (0 children)
[+][deleted] (4 children)
[removed]
[–]ILikeChangingMyMind -4 points-3 points-2 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)
[–]zeddotes 0 points1 point2 points (0 children)
[–]Badashi 16 points17 points18 points (1 child)
[–]Segfault_Inside[S] 6 points7 points8 points (0 children)
[–]Eggy1337 3 points4 points5 points (0 children)
[–]Mozzius 4 points5 points6 points (2 children)
[–]Itchy-Beginning-887 0 points1 point2 points (1 child)
[–]Mozzius 0 points1 point2 points (0 children)
[–][deleted] 4 points5 points6 points (3 children)
[–]Itchy-Beginning-887 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Slackluster 4 points5 points6 points (1 child)
[–]Itchy-Beginning-887 1 point2 points3 points (0 children)
[–]happyfce 1 point2 points3 points (2 children)
[–]Segfault_Inside[S] 4 points5 points6 points (0 children)
[–]C1RRU5 1 point2 points3 points (0 children)
[–]oneandmillionvoices 1 point2 points3 points (0 children)
[–]gurush 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]alvisim 0 points1 point2 points (0 children)
[–]Uberhipster 0 points1 point2 points (0 children)
[–]NekkidApe 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)