3

Here’s an excerpt from the start the jQuery source code (1.7.1):

// Define a local copy of jQuery
var jQuery = function( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context, rootjQuery );
    },

    // Map over jQuery in case of overwrite
    _jQuery = window.jQuery,

    // Map over the $ in case of overwrite
    _$ = window.$,


    // ...continues...


    // [[Class]] -> type pairs
    class2type = {};

The code is a series of variable assignments, all joined by the comma operator. As I understand the comma operator, it evaluates the expressions before and after it, and returns the value of the latter one.

As the chain of variable assignments isn’t being assigned to anything, it struck me that the code could be rewritten with semicolons in place of the commas, without changing how it operates. E.g.

// Define a local copy of jQuery
var jQuery = function( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context, rootjQuery );
    }; /* <----- semicolon */

    // Map over jQuery in case of overwrite
    _jQuery = window.jQuery; /* <----- semicolon */

    // Map over the $ in case of overwrite
    _$ = window.$; /* <----- semicolon */

    // ...and so on.
  1. Would the code have the same effect if rewritten with semicolons like this, or am I missing something?

  2. If it would have the same effect, what are the style reasons for writing it with commas? (I know, for example, that Crockford doesn’t like the comma operator (see near the bottom), although I don’t know why.)

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
  • 2
    In response to your edit, he says (emphasis mine): , (comma) Operator - Avoid the use of the comma operator except for very disciplined use in the control part of for statements. (*This does not apply to the comma separator*, which is used in object literals, array literals, *var statements*, and parameter lists.) – thirtydot Feb 29 '12 at 10:21
  • 2
    If you use semi-colon then need to write var for each variable, isn't it? – Brij Feb 29 '12 at 10:22
  • @thirtydot: doh, gotcha, good spot. Although in the section on variable declarations, he suggests one declaration per line. (Admittedly he’s just declaring there, rather than declaring and initialising.) – Paul D. Waite Feb 29 '12 at 10:25
  • You can do one declaration per line with a single `var` and a comma operator. It would be a single _statement_, but spread across several lines with one variable per line. Personally I find it messy to include comments on separate lines in amongst such declarations, because it makes it harder to see where the initial `var` is, and I find it messier still to include function declarations. – nnnnnn Feb 29 '12 at 10:31
  • See also http://stackoverflow.com/questions/3781406/javascript-variable-definition-commas-vs-semicolons and http://stackoverflow.com/questions/1236206/one-var-per-function-in-javascript – Paul D. Waite Feb 29 '12 at 10:48
  • Removed `jQuery` tag, cause this is pure js syntax problem/question. – kirilloid Feb 29 '12 at 12:19

4 Answers4

8

No. commas separate var declarations, all using first var prefix. E.g.:

var a = 1, // local
    b = 2; // local


var a = 1; // local
    b = 2; // global!

In order to achieve the same effect, write it this way:

var a = 1; // local
var b = 2; // local
kirilloid
  • 14,011
  • 6
  • 38
  • 52
  • *Ah* — oh dear, of course, can’t believe I missed that. Thank you, that answers 1. excellently. As for 2., I guess by using the comma operator they save on repeating `var `, which I guess could be a minor size-reduction tactic (*very* minor after gzipping). – Paul D. Waite Feb 29 '12 at 10:21
  • Repeating "var"s actually are somewhat "noisy" when one looking at the code... but making var global occasionally is error-prone. – kirilloid Feb 29 '12 at 10:29
2

Personally I think this is pure bad habit deformation and influence from the main language of the creator. The code becomes really hard to read and brings confusion. Especially when used in conjunction with a modular pattern where, in jQuery they declare everything on literal objects. jQuery seems to have forgotten how to declare a function constructor to instantiate a class or this simply does not apply to jQuery style.

The code will have the same effect if written with semicolon if you add a var in front of each variable:

// Define a local copy of jQuery
var jQuery = function( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context, rootjQuery );
    }; /* <----- semicolon */

    // Map over jQuery in case of overwrite
    var _jQuery = window.jQuery; /* <----- semicolon */

    // Map over the $ in case of overwrite
    var _$ = window.$; /* <----- semicolon */

    // ...and so on.

If you omit the VAR you will create or overwrite a global variable.

CaptainBli
  • 4,121
  • 4
  • 39
  • 58
elmuchacho
  • 424
  • 3
  • 8
0

I think this to comply with JSLint rules.

JSLint wants variable declarations separated by a comma rather than a semi-colon.

I'm not sure why Crockford denouces the comma operator when his tool encourages it. The message itelf reads:

Combine this with the previous 'var' statement.

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
  • 1
    right, but that's an instance of the comma *separator*. "Avoid the use of the comma operator except for very disciplined use in the control part of for statements. (This does not apply to the comma separator, which is used in object literals, array literals, var statements, and parameter lists.)" – benesch Mar 04 '12 at 07:30
0

I think this has several reason: one efficiency, second code tidiness and third variable scoping.

reason 1: if you specify: var var1,var2,var3; , the engine knows you're passing a list of variables because you're preceding the list with the "var" keyword. if you would do

var var1;
var var2;
var var3;

the engine would look at the var keyword, understand it's defining variables and then look at the argument. i guess looping through a list is better than re-evaluating var every time

reason 2: For me it's much more readable to read a list separated by commas than looking at equal sign somewhere in the code.

reason 3: note that var defines the variable inside the scope of a function. if you don't preceed with var you'll be defining the variables in the global scope.

p.s - jslinters like jshint often complain if you have multiple vars inside a scope rather than one list of variables....

cheers

Avi
  • 155
  • 8
  • On reason 1, in the comments of [this blog post](http://wonko.com/post/try-to-use-one-var-statement-per-scope-in-javascript), the author notes that over 300,000 iterations there doesn’t seem to be any performance benefit for typing `var` once as opposed to typing it multiple times. – Paul D. Waite Feb 29 '12 at 10:46
  • 1
    I stand corrected. Made some tests of my own with changing the amount of variable (value size and amount) - in most cases the ; notation was actually faster(chrome). but on milliseconds for 1,000,000 iterations. – Avi Feb 29 '12 at 12:39
  • nice one — the first rule of performance optimisation is measure. – Paul D. Waite Feb 29 '12 at 14:02