Javascript single var pattern. Am I overloading it?

I read in the book of Stoyan Stefanov about one var template. It is also good JSLint.

But I noticed in my code that I can overload this template. It looks like my whole .js file, a complete script, is just one big var.

For instance:

$(function(){ 

var
    some_var1   = 'some_value',
    some_var2   = { /* some other value */},

    // more and more vars 

    tiny_fun    = function(){ /* some tiny helper function */ },
    tiny_fun2   = function(){ /* another tiny helper function */},

    // more tiny functions

    Constructor1    = function(){ /* Some Constructor */ },
    Constructor2    = function(){ /* Another Constructor */ },

    script_body     = (function(){

        // main script - 'script body'

        var c1 = new Constructor1();
        c1.some_method();

        // and other client code            
    })(); //: script_body
});

This is bad? maybe I misunderstood this single-var pattern and should use it only for variables - to prevent the use of global variables?

+4
source share
1 answer

If you are only grouping private access elements, there is no problem. Remember that you cannot declare public access items with var(global scope is an exception).

+2

All Articles