News for hackers. Stuff that matters.
Re:Idle Until Urgent by theandrewbailey on Thursday September 20, 2018 @08:51AM (#18031951)
I guess I'm an old man that hasn't smoked the hype, but I don't understand why one would write this:

    const main = () => {
        setTimeout(() => drawer.init(), 0);
        setTimeout(() => contentLoader.init(), 0);
        setTimeout(() => breakpoints.init(), 0);
        setTimeout(() => alerts.init(), 0);
        requestIdleCallback(() => analytics.init());
    };
over this:

    function main(){
        setTimeout(drawer.init, 0);
        setTimeout(contentLoader.init, 0);
        setTimeout(breakpoints.init, 0);
        setTimeout(alerts.init, 0);
        requestIdleCallback(analytics.init);
    };
Re:Idle Until Urgent by rbonvall on Thursday September 20, 2018 @11:32AM (#18033182)
JavaScript is a minefield of unexpected behavior when you try things like this. Besides the issue with this-binding already mentioned in the thread, there are other examples of weird stuff that happens when you don't introduce an apparently redundant lambda:

    > ['10', '10', '10', '10', '10'].map(parseInt)
    [ 10, NaN, 2, 3, 4 ]

    > ['10', '10', '10', '10', '10'].map(x => parseInt(x))
    [ 10, 10, 10, 10, 10 ]
Re:Idle Until Urgent by infogulch on Thursday September 20, 2018 @11:59AM (#18033434)
Ok this was a 'wat' moment for me until I realized that map passes up to 3 args to the function, including index, and parseint accepts a radix parameter. Still kinda weird/unintuitive, I could see myself not paying attention and reducing to the first example accidentally.
Re:Idle Until Urgent by kristjansson on Thursday September 20, 2018 @12:18PM (#18033568)
> I’ve heard something about a surprise principle in API design. Principle of most surprise I think it was? Let’s go with that.

The design process of this, I imagine.

Re:Idle Until Urgent by Serow225 on Thursday September 20, 2018 @01:22PM (#18034067)
I lol'd. Downvote away haters, sometimes you gotta laugh.
Re:Idle Until Urgent by jcelerier on Thursday September 20, 2018 @11:57AM (#18033418)
I'll take C++ UB every day over this
Re:Idle Until Urgent by rictic on Thursday September 20, 2018 @02:28PM (#18034505)
Huh, I'm the opposite. I'd gladly take a confusing combination of consistent spec'd behavior over time travel, arbitrary code execution, and nasal demons.
Re:Idle Until Urgent by jcelerier on Friday September 21, 2018 @10:59AM (#18040164)
> I'd gladly take a confusing combination of consistent spec'd behavior over time travel, arbitrary code execution, and nasal demons.

the point of UB is that you can tell your compiler to transform it into a specific behaviour -e.g., an assert with -fsanitize=undefined for instance.

Re:Idle Until Urgent by marcosdumay on Thursday September 20, 2018 @05:38PM (#18035674)
Well, at least with a consistent spec you can create a compiler for a good language.

Somehow, undefined behavior in C is a much smaller problem than it looks like. But the principle still applies.

Re:Idle Until Urgent by streptomycin on Thursday September 20, 2018 @08:53AM (#18031978)
The 2nd style will not work if those functions internally use `this` and were not previously manually bound to their parent objects, like `drawer.init.bind(drawer)`.
Re:Idle Until Urgent by taneq on Thursday September 20, 2018 @09:20AM (#18032187)
Presumably if you were going to write the latter, you'd also write your init() functions accordingly?
Re:Idle Until Urgent by mason55 on Thursday September 20, 2018 @09:21AM (#18032197)
If you didn’t write the init functions you don’t have a choice
Re:Idle Until Urgent by taneq on Thursday September 20, 2018 @11:19AM (#18033079)
If we were talking about a different situation then we'd be discussing a different article.
Re:Idle Until Urgent by Xichom2k on Thursday September 20, 2018 @08:55AM (#18031991)
Due to javascript's funky notion of object methods. If you just pass the function reference itself invoking it will execute it with a |this| set to undefined.

Put differently, a property access x = foo.bar followed by x() is not the same as foo.bar()

Re:Idle Until Urgent by avip on Thursday September 20, 2018 @09:09AM (#18032103)
The default this context would be the global window object (in the browser).
Re:Idle Until Urgent by Xichom2k on Thursday September 20, 2018 @09:12AM (#18032126)
Not in strict mode
Re:Idle Until Urgent by avip on Thursday September 20, 2018 @09:00AM (#18032026)
This.
Re:Idle Until Urgent by ak39 on Thursday September 20, 2018 @10:13AM (#18032556)
How long have you waited for this?
Re:Idle Until Urgent by bryanrasmussen on Thursday September 20, 2018 @10:43AM (#18032759)
probably as long as I've waited for that.
Re:Idle Until Urgent by ealhad on Thursday September 20, 2018 @10:15AM (#18032573)
I can't figure out if the pun was intended.
Re:Idle Until Urgent by tome on Thursday September 20, 2018 @10:35AM (#18032709)
Come on, give avip some credit! That's one of the best comments ever to appear on HN.
Re:Idle Until Urgent by ealhad on Thursday September 20, 2018 @10:53AM (#18032851)
Only because of the context.
Re:Idle Until Urgent by dwaltrip on Thursday September 20, 2018 @04:35PM (#18035247)
All greatness depends on the context.
Re:Idle Until Urgent by ealhad on Friday September 21, 2018 @03:28AM (#18037821)
Especially “this”.
Re:Idle Until Urgent by Cthulhu_ on Thursday September 20, 2018 @11:01AM (#18032914)
The second argument (0) is also optional, 0 is the default value.