4 ms·
I am not experienced with javascript, so I don't understand this argument. Can someone please confirm the following, or explain what's going on? Here is what
by leif 15y ago
I am not experienced with javascript, so I don't understand this argument. Can someone please confirm the following, or explain what's going on?
Here is what I understand:
1. Javascript will add a phantom semicolon at the end of each line if it seems like you need it, for some definition of seems like. To me, it sounds like this is so that new HTML writers in '98 wouldn't see their pages blow up when they forgot a semicolon at the end of a script. It doesn't sound like professional programmers are expected to leave semicolons out on purpose (this is the opposite from python). The bootstrap authors took advantage of this because somehow screwing with the compiler is in vogue.
2. Crockford's tool broke when a semicolon was left out somewhere. I guess he didn't implement a proper js compiler that covers the entire language spec.
3. There's some business about unary and binary ! operators? Something about sticking a ! in front of your line lets you leave the semicolon off the end? This sounds utterly braindead to me. It's no fewer characters, and the thing you're typing is going really far away (at the beginning of a big block of code) from where it's actually meaningful to you (at the end). Also, it looks like it relies on some fairly convoluted unintended consequences of the spec, that may not be future-proof.
Here is my analysis, if the above understanding is true:
1. Bootstrap needs to fix their code. This business of the compiler sticking in semicolons where it thought you meant them is a crutch to prevent stupid errors from being fatal errors in the average case, but that doesn't mean they aren't errors. Be explicit.
2. Crockford should fix his tool soon, not complain about other people's code, and say "I'll fix it soon, but for now, here's a workaround: just put a semicolon in your code".
3. What the hell. Are people really doing this? Is the web one big obfuscated javascript competition? What happened to writing simple, explicit, readable, maintainable code? Are we really bickering over single characters and trying to play subtle tricks with language esoterica to save them like a bunch of 14-year old perl newbies? Man, I hope these morons aren't in charge of my bank account.
Overall, I'm with Crockford, but he doesn't need to be this much of a jerk about it.
Further analysis, now that I've read it with a decent (non-cell) data connection: allowing gifs in a public issue tracker is a terrible decision.
- chris_wot 15y agoFor point 3, it might be worthwhile understanding what the ECMA standard says about automatic semi-colon insertion. It won't happen for the following: a = b ++c This becomes: a = b; ++c; and NOT: a = b++; c; There are other, odd gotcha's, but this is the one being discussed. I believe the issue with the bang (!) operator is that it's being considered as a binding de-reference of function objects. See here: https://mail.mozilla.org/pipermail/es-discuss/2010-July/011494.html https://mail.mozilla.org/pipermail/es-discuss/2010-July/0114... Therefore, the problem with the code would be that an automatic semi-colon would not be inserted and the code, which is currently: clearMenus() !isActive && $parent.toggleClass('open') Would become: clearMenus()!isActive && $parent.toggleClass('open'); Instead of: clearMenus(); !isActive && $parent.toggleClass('open'); That was my understanding, at any rate.
- mattbriggs 15y agohttp://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding http://blog.izs.me/post/2353458699/an-open-letter-to-javascr... That is a pretty good rundown of the rules. The only problem with not having semicolons is cases like this, where someone is using a bad tool to minify your code. None of the tools that currently do the best job will break on that code.
- leif 15y agoYes, tool writers need to understand this behavior and handle it. But I still don't understand why the hell you would want to do this. These are a lot of rules to remember, and I don't think it makes sense to think about them for each statement I write. Nothing here says "here is what you gain with this technique", all I see is "it's perfectly valid so quit whining". This doesn't satisfy my curiosity. Then we have this: For example, instead of this: foo(); [1,2,3].forEach(bar); you could do this: foo() ;[1,2,3].forEach(bar) The advantage is that the prefixes are easier to notice, once you are accustomed to never seeing lines starting with ( or [ without semis. So now the argument is "don't worry, you get used to writing stupid looking code"? This is getting depressing.
- mattbriggs 15y agostarting a line with a semicolon means "start a new expression", if it is done where it is meaningful. By putting it at the start of the line, it is easier to associate it with the statement that makes the character important, and calls out that you are paying attention to one of the biggest gotchas in the language putting a semicolon at the end of every line is basically just noise. You get used to it, and I find semi-colon bugs end up happening much more frequently. More then that, you are essentially just typing an arbitrary character 99.99999% of the time. When you need a semi colon is essentially a corner case, so the vast majority of code is just getting appended with a character which has no meaning. Its like the javascript community decided to surround all expressions with parens, since it makes things more explicit, and there are a few cases (much more common then semi colons) where you need it in the language. Or if they decided to end all lines with //, just in case someone wanted to follow it with a comment. Those two examples make as much sense as advocating semicolons at the end of each line.