News for hackers. Stuff that matters.
Re:Writing great Javascript by funthree on Friday March 23, 2012 @10:28PM (#3748274)
You really only need one or the other depending on the style of the code you are using in the rest of your project.

If you write js using semi-colons everywhere:

    (function () { })(this);

    (function () { })(this);

    (function () { })(this);
If you dont want to use semi-colons unless you have to:

    ;(function () { })(this)

    ;(function () { })(this)

    ;(function () { })(this)
Basically, never start lines with `(` or `[` without semis and don't throw in line breaks liberally without understanding what semicolon insertion is going to happen.

http://blog.izs.me/post/2353458699/an-open-letter-to-javascr...

Re:Writing great Javascript by user24 on Saturday March 24, 2012 @05:09AM (#3748901)
But what if you do this:

    <script src='3rdpartyA.js'></script>
    <script src='yourscript.js'></script>
    <script src='3rdpartyB.js'></script>
You need the semicolon at both ends to insulate your script from the unknown coding style or 3rd party scripts which may be appended or prepended to yours.

If 3rd party A omits a trailing semicolon, or 3rd party B doesn't uses your semicolon prefix style then by wrapping your script in semicolons you pre-emptively prevent problems.

I believe this is an example of the wider coding style called "defensive coding".

Re:Writing great Javascript by funthree on Saturday March 24, 2012 @12:05PM (#3749689)
Or you could actually just read the source code of the libs you are using...
Re:Writing great Javascript by esrauch on Saturday March 24, 2012 @12:10PM (#3749705)
It's pretty easy to miss a terminating semicolon which will waste time, but the real use case is for people who are writing the libs that other people are using; they have absolutely no knowledge or control of what other javascript might be prependend or appended onto theirs.
Re:Writing great Javascript by user24 on Saturday March 24, 2012 @08:09PM (#3751232)
Yeah. So I add a trailing semicolon. Then the 3rd party library releases an update. I have to remember to merge my patch every time they update, do I?