Live data from Hacker News

Writing great Javascript

oli.me.uk

1–10 of 12 posts

Re: Writing great Javascript

#3
I've never seen the preceding semi-colon like that. It makes perfect sense, there are a lot of JavaScript coders these days coming from languages like Ruby and these people often don't use semi-colons, very smart advice to protect against that!

Re: Writing great Javascript

#4

I've never seen the preceding semi-colon like that. It makes perfect sense, there are a lot of JavaScript coders these days coming from languages like Ruby and these people often don't use semi-colons, very smart advice to protect against that!

The preceding semi-colon is definitely a very smart advice. I also will have to try out jshint (I've been using jslint).

Re: Writing great Javascript

#5

I've never seen the preceding semi-colon like that. It makes perfect sense, there are a lot of JavaScript coders these days coming from languages like Ruby and these people often don't use semi-colons, very smart advice to protect against that!

Adding a leading semicolon just in case seems like overly defensive programming to me. You ought to pick trustworthy tools. If your script concatenator doesn't work right, I think it's better to fail fast rather than let things quietly be half-broken all over the place.

Re: Writing great Javascript

#6
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

#7
post #6

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…

But what if you do this:

    
    
    
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

#8

I've never seen the preceding semi-colon like that. It makes perfect sense, there are a lot of JavaScript coders these days coming from languages like Ruby and these people often don't use semi-colons, very smart advice to protect against that!

It doesn't even need to be a semicolon. It's also common to see a ! used for this purpose.

Re: Writing great Javascript

#9
post #5

I've never seen the preceding semi-colon like that. It makes perfect sense, there are a lot of JavaScript coders these days coming from languages like Ruby and these people often don't use semi-colons, very smart advice to protect against that!

Adding a leading semicolon just in case seems like overly defensive programming to me. You ought to pick trustworthy tools. If your script concatenator doesn't work right, I think it's better to fail fast rather than let things quietly be half-broken all over the place.

You ought to pick trustworthy tools.

Many JavaScript snippets are made to be integrated and added by other people on their websites.

Re: Writing great Javascript

#10
post #7
post #6

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…

But what if you do this: 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…

Or you could actually just read the source code of the libs you are using...
Post reply on HN