12 ms·
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.i
by theandrewbailey 8y ago
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);
};
- streptomycin 8y agoThe 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)`.
- Xichom2k 8y agoDue 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()
- avip 8y agoThis.
- ealhad 8y agoI can't figure out if the pun was intended.
- ak39 8y agoHow long have you waited for this?
- bryanrasmussen 8y agoprobably as long as I've waited for that.
- Cthulhu_ 8y agoThe second argument (0) is also optional, 0 is the default value.
- deleted 8y ago[deleted]
- rbonvall 8y agoJavaScript 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 ]
- jcelerier 8y agoI'll take C++ UB every day over this
- rictic 8y agoHuh, 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.
- marcosdumay 8y agoWell, 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.
- jcelerier 8y ago> 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.
- infogulch 8y agoOk 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.
- deleted 8y ago[deleted]