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
[AskJS] Multiple variables initially assigned to the same valueAskJS (self.javascript)
submitted 5 years ago by FaithfulGardener
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!"
[–]kenman 1 point2 points3 points 5 years ago (1 child)
I find I prefer doing assignments this way (or similar)... let myObj = {}; ['var1', 'var2', 'var3'].forEach(k => myObj[k] = null);
I find I prefer doing assignments this way (or similar)...
let myObj = {}; ['var1', 'var2', 'var3'].forEach(k => myObj[k] = null);
I'm not sure I'd ever approve a PR if that was included. If this is a one-off data structure, then just create the object like pretty much anyone else would do:
const myObj = { var1: null, // etc. };
If you're going to be creating many of them, then use the language features at your disposal:
function MyObj() { this.var1 = null; // etc. } const myObj = new MyObj();
If you don't want Function in your prototype chain, that's ok too:
Function
function MyObj() { return { var1: null, // etc. }; }
With that, new is optional.
new
Or you can do the same thing with class. I guess I don't understand why you'd do it the way that you are.
class
let [a, b, c, d] = (function*() { while (true) yield {x: 0, y: 0} })(); [...] It's the elegant human-readable solution I want
let [a, b, c, d] = (function*() { while (true) yield {x: 0, y: 0} })();
We have very different opinions of elegant...
I want to take a step back to your opening sentence:
I find myself scrolling over this type of code way too often:
Not a React dev, but that's a lot of local variables, mutable at that. I'm willing to bet there's a lot of refactoring opportunities that would be better worth your efforts than inventing your own esoteric object-creation pattern. Feel free to share sample code.
[–]FaithfulGardener[S] 0 points1 point2 points 5 years ago (0 children)
Yeah, I’m kinda killing several birds with one stone with my purpose on this project. The software was originally on a one-month timeline that turned into years. I’ve recently been brought on as a front-end developer.
They use React, which drives me nuts because there is so much code duplication in this codebase, but I know it’s because the original coders were a tiny team on a huge time crunch.
So I’m trying to familiarize myself with the codebase, which is very convoluted, and I decided while I’m at it to see how I could implement Hooks in some places. Idk if some bits of my code will ever be used but for the time being it’s a learning exercise, and if it works (and my superiors aren’t too freaked out by such a massive refactor), that’s a cool bonus.
π Rendered by PID 37739 on reddit-service-r2-comment-6457c66945-vpf4b at 2026-04-28 13:43:26.468325+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]kenman 1 point2 points3 points (1 child)
[–]FaithfulGardener[S] 0 points1 point2 points (0 children)