4

I've read in Stoyan Stefanov book about single var pattern. Also it's good by JSLint.

But I've noticed in my code that I maybe overload this pattern. It occurs that whole my .js file, whole script is just one big var.

For example:

$(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
});

Is it bad? maybe I've misunderstood this single-var pattern and should use it only for variables - to prevent using globals?

1 Answer 1

2

If you are only grouping private access elements there is no problem at all. Remember that you can't declare public access elements using var (global scope is an exception).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.