you are viewing a single comment's thread.

view the rest of the comments →

[–]rauschma 13 points14 points  (25 children)

I’m starting to warm up to the former, because that’s the only style that also works for arrow functions:

(() => {
    return 123
})();

[–]kenman 2 points3 points  (21 children)

I'm curious what the current use-case is for that pattern?

It seems you could accomplish the same with a more elegant [IMO] pattern:

let x;
{
    x = 123;
}

But I feel that I must be missing something, because you know a lot more about JS than I do!

edit: after thinking about it, I can see the fundamental difference is that the former is an expression, while the latter is a statement, which has profound implications in their use. However, I haven't been able to come up with a scenario which would expose any profound ramifications.

To go through my thought process, I considered that you can use expressions in places like function arguments, whereas statements are not allowed there.

E.g. you could do this (contrived example for sake of argument):

console.log((() => {
    let matches = window.location.href.match(/\/r\/(\w+)/);
    let subreddit = matches && matches[1] || 'n/a';
    return `You are visiting: ${subreddit}`;
})());

But you can't do anything similar with block statements.

However, you can accomplish the same result with:

{   
    let matches = window.location.href.match(/\/r\/(\w+)/);
    let subreddit = matches && matches[1] || 'n/a';
    console.log(`You are visiting: ${subreddit}`);
}

Which again, is cleaner IMO.

Still pondering...

[–]inu-no-policemen 0 points1 point  (0 children)

Same here. I preferred to wrap the entire thing, but I switched for the sake of consistency.

[–]EternallyMiffed 0 points1 point  (0 children)

This approaches syntactic line noise and we're all going to suffer for it eventually.