Hacker News new | past | comments | ask | show | jobs | submit login

It does bite you if you are not careful, for example:

    (function(){
        var foo = 1;
        console.log(foo);
    })() // Semicolon missing!

    (function(){
        var bar = 1;
        console.log(bar);
    })()
It's ambigous for the parser so this will throw a TypeError: "undefined is not a function". So overall I don't think is a stylistic choose but a safety one.



Sure, but the solution should not be "semicolon everywhere" but rather "semicolon where necessary", where necessary also includes places that can produce ambiguities depending on the code directly following (or after minification). Omitting a semicolon after a function declaration will never produce ambiguous code.

What I'm still arguing for is that "enforcing semicolon habits" is not a compelling argument for using function expressions over function declarations. (Perhaps you're talking about semicolons in general, which is something I would rather not get into :)


Everywhere where it _can_ create an ambiguity (regardless if it does or not); having to look at previous unrelated code to see if you need or not a semicolon is inefficient; better put it everywhere where any adjacent code can create ambiguity.

And function declarations can create ambiguous code, because functions are first class citizens on JavaScript it means code like this is valid:

    var foo = function (bar){
        console.log(bar);
    }
but for the lacking semicolon it throws an error if it is followed by:

    (function(){
        var foo = 1;
        console.log(foo);
    })()


Rather than putting a semi-colon after every function declaration, I would suggest the Lua convention -- put the semi-colon at the beginning of the line that could cause issues. An empty statement is valid is JavaScript, so this should work in all cases I can think of:

    ;(function(){
        var foo = 1;
        console.log(foo);
    })()
In Lua, whitespace is very nearly all the same. For example:

    a = 1 b = 2

    c
    =
    3
is valid Lua code. However, lines that start with parens have the same ambiguity:

    a = something
    (expression)(arguments)

    --Could be:
    a = something(expression)(arguments);
    --Also could be:
    a = something;
    (expression)(arguments);
The parser will complain about the ambiguity. If the intention is to have 1 statement, they should probably be on the same line, otherwise the convention is to put the semi-colon at the beginning of the second line:

    a = something
    ;(expression)(arguments)


That would be a change of paradigm because no JavaScript standard -or library- does that. Plus it feels wrong because the spoken language equivalent would be something like:

    The quick brown fox jumps over the lazy dog
    . The fox didn't say anything that day
    , because in all honesty what does the fox say
    ?


I think I've been unclear. When I say function definition I'm referring to this:

    function foo() {
      console.log('bar');
    }
And when referring to a function expression I'm talking about this:

    var foo = function () {
      console.log('bar');
    };
The latter needs to be terminated with a semicolon to prevent ambiguity (in some situations), while the former does not. This for example is valid JavaScript:

    function foo() {
      console.log('bar');
    }foo=5


Yeah, that's correct; no discussion on that, and no linter that I have used asks you to put a semicolon in such case.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: