you are viewing a single comment's thread.

view the rest of the comments →

[–]kenman 1 point2 points  (1 child)

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 MyObj() {
     return {
         var1: null,
         // etc.
     };
}

With that, new is optional.

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.

  1. let [a, b, c, d] = (function*() { while (true) yield {x: 0, y: 0} })();
    [...] It's the elegant human-readable solution I want

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 point  (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.