7

I've been developing a plugin for jQuery "jQueryLog" to allow for debugging of chain selectors and return values. If you want to check it out, you can do it here

This is already a second version. The first version was actually an edited jQuery and while doing it I had to read jQuery to understand how the internals worked. The question comes from there:

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.$,

    // A central reference to the root jQuery(document)
    rootjQuery,

    // A simple way to check for HTML strings or ID strings
    // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
    quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
    (...)

Is there any big reason for the using a chain of declarations + "comma" instead of just using:

function jQuery ( selector, context ) { ... }

var _jQuery = window.jQuery;
var _$ = window.$;
etc...

The only reason I see here is for the minifier to have less literals that can't be cut down. But are there any other reasons?

fmsf
  • 36,317
  • 49
  • 147
  • 195
  • 1
    Read this [link](http://stackoverflow.com/questions/3781406/javascript-variable-definition-commas-vs-semicolons) – Ketan Modi Dec 26 '11 at 18:35
  • 2
    I don't know that using commas over 'var' has ever been declared an industry 'best practice'. At best, this is a stylistic preference. I'd be interested in reading any article you could provide that advocated this as a best-practice, though. – Daniel Szabo Dec 26 '11 at 18:42
  • @Ronak why don't you just say it's a duplicate question? – kojiro Dec 26 '11 at 18:42
  • @ajax81 I guess if you consider JSLinting to be a best practice, then any recommendation it makes would also be a best practice. – kojiro Dec 26 '11 at 18:43
  • OK... This' duplicate question. – Ketan Modi Dec 26 '11 at 18:50
  • just read this, I've added this question to the dupes. so my initial guess was right. Thought it might have anything else to it – fmsf Dec 26 '11 at 18:58

1 Answers1

5

It's just a shorter way to keep all the variables in the function scope while making sure they aren't used before they're defined.

In JavaScript Patterns (Sept. 2010, O'Reilly), Stoyan Stefanov calls it the single var pattern:

JavaScript enables you to have multiple var statements anywhere in a function, and they all act as if the variables were declared at the top of the function. This behavior is known as hoisting. ... You use one var statement and declare multiple variables delimited by commas. It's a good practice to also initialize the variable with an initial value at the time you declare it. This can prevent logical errors (all uninitialized and declared variables are initialized with the value undefined) and also improve code readability.

parisminton
  • 401
  • 1
  • 6
  • 13