Javascript 单 var 模式.我超载了吗? [英] Javascript single var pattern. Am I overloading it?

查看:20
本文介绍了Javascript 单 var 模式.我超载了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Stoyan Stefanov 中读过关于单变量模式的书.JSLint 也很好.

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

但是我在我的代码中注意到我可能重载了这个模式.发生整个我的 .js 文件,整个脚本只是一个大变量.

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.

例如:

$(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?

推荐答案

如果您只是对私有访问元素进行分组,则完全没有问题.请记住,您不能使用 var 声明公共访问元素(全局范围是一个例外).

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

这篇关于Javascript 单 var 模式.我超载了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆