开发者

Javascript single var pattern. Am I overloading it?

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(){ 开发者_C百科/* 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?


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜