News for hackers. Stuff that matters.
Re:Twitter crashes itself with commas by pavel_lishin on Tuesday July 17, 2012 @11:16AM (#4256013)
I'm willing to, please don't withhold enlightenment to spite one guy.
Re:Twitter crashes itself with commas by tomjen3 on Tuesday July 17, 2012 @11:37AM (#4256150)
I write Javascript for a living, and the code standard requires semicolons.

Anyway variables in javascript have function scope, so two variables with the same name in the same function always refer to the same value, even if declared twice.

As a result I like to write all the variable statements at the top of each function, all in one var statement.

The problem then becomes that you write var a, b, c, d; and then later want to add variable e. If you aren't careful you might accidentally write var a, b, c, d; e; if you do that, the program may still work but e now has global scope. This results in nearly impossible to find bugs that may only happen if the function is recursive or it is called (perhaps indirectly) by some other function with a similarily named variable.

Considering that semicolons insertion mostly works, and the other can be a huge pain in the rear end, I can understand why people would want to skip the semicolons.

Re:Twitter crashes itself with commas by DrJokepu on Tuesday July 17, 2012 @12:35PM (#4256485)
I think it's generally a good idea to avoid declaring multiple variables in the same line (unless you have a very good reason). The space you "save" isn't worth the maintainability penalty. This is a habit I have from C where

  int* a, b
declares "a" as ( int* ) and b as ( int ). Instead, I just write

  int *a;
  int *b;
Re:Twitter crashes itself with commas by lmm on Wednesday July 18, 2012 @05:31AM (#4260041)
If you get in the habit of writing the * next to the variable then you can't make the mistake. int *a, b, c[]; perfectly clear.
Re:Twitter crashes itself with commas by tedunangst on Tuesday July 17, 2012 @11:50AM (#4256242)
If you aren't careful, you might accidentally write var a, b, c, d, r;
Re:Twitter crashes itself with commas by thedaniel on Tuesday July 17, 2012 @11:52AM (#4256253)
The JS interpreter shares your preference of writing all var declarations at the top - in fact, at interpretation time they will be "hoisted" to the top of the scope. In other words:

  foo()
  var bar = 1;
is actually run as

  var bar;
  foo();
  bar = 1;
Re:Twitter crashes itself with commas by tomjen3 on Tuesday July 17, 2012 @12:37PM (#4256489)
True, and that was part of what I was trying to articulate.
Re:Twitter crashes itself with commas by OriginalSyn on Wednesday July 18, 2012 @12:49PM (#4261597)
But this is a simple error caught by js(h/l)int. And it's especially easy to spot if you just declare each var one after another:

    var a,
        b,
        c,
        d;
        e;
         
Then your editor (which you've a integrated a linter... right?) will protested:

    yourJsFile.js |5 warning| 'e' was used before it was defined.
And if you have a global by the same name you'll see:

    yourJsFile.js |5 warning| Expected an assignment or function call and instead saw an expression.
Granted, if you assign it a default value then, yes you're going to have a bad time. But if you follow the white spacing rules of jsLint it will catch the obvious error.
Re:Twitter crashes itself with commas by bmuon on Tuesday July 17, 2012 @02:08PM (#4256942)
Strict mode should save you from a lot of those.

    (function () {
      'use strict';
      var a = 1; b = 2;
    }());
    // ReferenceError: b is not defined