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.
Would the code have the same effect if rewritten with semicolons like this, or am I missing something?
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.)