gpt4 book ai didi

Javascript 单一 var 模式。我重载了吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:21:43 27 4
gpt4 key购买 nike

我在 Stoyan Stefanov 的书中读到了关于单一变量模式的内容。 JSLint 也很好。

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

例如:

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

不好吗?也许我误解了这种单变量模式,应该只将它用于变量 - 以防止使用全局变量?

最佳答案

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

关于Javascript 单一 var 模式。我重载了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6235417/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com