3
lopu
68d

Javascript really needs a way to define consts for a block 1 level up.

So instead of

const el = wide ? els.wide : els.tall

You can do

if (wide) const el = els.wide
if (!els) const el = els.tall

// etc..

Just makes chaining and backup values way easier than inline conditionals 🤔

Comments
  • 1
    For example at the moment we have to do this..

    let rewardPointsContent;

    if (isShowMuff && !rewardPointsContent) {

    rewardPointsContent = <Muffs />;

    }

    if (isEaster && !rewardPointsContent) {

    rewardPointsContent = <HCBEaster />;

    }

    if (!rewardPointsContent) {

    rewardPointsContent = <IcedCoffee />;

    }
  • 0
    like hoisting consts? 😭
  • 5
    sounds like you need to use a feature flag checker which is...

    a function that returns the value that should be in the const :P

    if (isConst) return skill.issue;
    if(isEaster) return egg.issue;
    //And more
  • 5
    I think it's not nice to declare a var in an if and using it outside it
  • 2
    @retoor lol, I didn‘t even know that it was allowed. Gotta love JS: It never runs out of little details which are plain idiotic. 😂
  • 3
    @Lensflare should I tell you about "global" keyword in python and why it is necessary
  • 3
    @electrineer sure, I‘m always interested in learning new jokes
  • 1
  • 2
  • 0
    @melezorus34 this is the correct answer.
  • 0
    @electrineer wow, nice stuff
  • 1
    Maybe I’m not understanding but..

    you can always use a global

    or define a function as a a child of a another function - which allows acess to parent scope

    Beyond stuff like that - do any languages allow more than that?
  • 0
    @melezorus34 nice nice good point but still, easier method would be good 🙏
  • 0
    @lopu look, it sounds good to be able to do that but it's a nightmare on many points.

    We can bikeshed everyshit until you are satisfied but the most SIMPLE, closest to what you gave as an example and not "introducing overhead" way probably is this:

    const el = flag1 && value1
    || flag2 && value2
    //go on until satisfied and PROVIDE A FALLBACK DAMMIT

    this way:
    - you are not only making the javascript code schrodinger's code++™, where a variable may:
    1. not be defined
    2. defined once, but for type loving users like me, it's type cannot be determined easily if the flags are tied to runtime conditions
    3. defined more than once, which, oh god how do i put this uh, we want PEOPLE to write the compiler for this. please don't make us get two masters degree for fucking working on JS. Pretty please, even.
    - still not introducing a new function
    - not getting on my and many others' nerves :)
  • 0
    AND NO, BEFORE YOU SAY "but i wanna define my own way, i don't want to be limited by the ugliness of boolean collapsing" OR SOMETHING LIKE THAT

    I ALREADY SAID YOU CAN USE A fhucking METHOD FOR THAT PURPOSE.

    Now go touch grass before I make a meme about this
Add Comment