Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript全局设计模式_Javascript_Jquery_Globals - Fatal编程技术网

JavaScript全局设计模式

JavaScript全局设计模式,javascript,jquery,globals,Javascript,Jquery,Globals,可能重复: 我正在寻找一些关于如何最好地在JavaScript中管理全局变量的建议 考虑以下几点: foo = 1; bar = 2; // Update our global variable function doStuff (newFooValue) { foo = newFooValue } // Update a global based on a condition function doMoreStuff () { if (bar == 3) {

可能重复:

我正在寻找一些关于如何最好地在JavaScript中管理全局变量的建议

考虑以下几点:

foo = 1;
bar = 2;

// Update our global variable
function doStuff (newFooValue) {
    foo = newFooValue
}

// Update a global based on a condition
function doMoreStuff () {
    if (bar == 3) {
        foo = 1;
    }
}
在这两种情况下,我们的函数都在内部访问全局变量,这让我觉得很难看。从我所读到的内容来看,我们希望尽可能避免全局名称,以避免阻塞全局名称空间

那么,我们需要的是为我们的全球人创造一个结构吗

比如说,

var myPage = {}
myPage.foo = 1;
myPage.bar = 2;
我想这解决了全局名称空间冲突问题,但我仍然从方法中访问全局范围


我应该怎么做?

为了避免全局名称空间污染,您应该将代码包装在。JavaScript变量具有函数作用域,因此它们只存在于声明它们的函数中

(function () {
    //these variables only exist within the outer function
    var foo,
        bar;
    foo = 1;
    bar = 2;

    //these functions only exist within the outer function
    function fizz() {
        return foo + bar;
    }
    function buzz() {
        return foo - bar;
    }
}());
上面的例子是非常无用的,因为所有的变量和函数都是封装的,不能在外部使用。任何需要全局的函数/变量都应手动添加到全局对象:

(function (g) {
    //not repeating code from above, pretend it's here

    //make the functions globally accessible
    g.fizz = fizz;
    g.buzz = buzz;
}(this));

为了避免全局名称空间污染,您应该将代码包装在一个名称空间中。JavaScript变量具有函数作用域,因此它们只存在于声明它们的函数中

(function () {
    //these variables only exist within the outer function
    var foo,
        bar;
    foo = 1;
    bar = 2;

    //these functions only exist within the outer function
    function fizz() {
        return foo + bar;
    }
    function buzz() {
        return foo - bar;
    }
}());
上面的例子是非常无用的,因为所有的变量和函数都是封装的,不能在外部使用。任何需要全局的函数/变量都应手动添加到全局对象:

(function (g) {
    //not repeating code from above, pretend it's here

    //make the functions globally accessible
    g.fizz = fizz;
    g.buzz = buzz;
}(this));

也许可以把它放在课堂上

var myPage = {}
myPage.foo = 1;
myPage.bar = 2;
myPage.doStuff = function(newFooValue) {
    this.foo = newFooValue;
}
你只需要在全球范围内使用一个点。 您应该避免这样做,但是如果您需要处理全局范围,那么您将这样做。 如果你问的是设计模式,你必须更加精确。
如果您有两个名为doStuff和domorseuff的函数,那么设计起来就非常困难。

是否可以将其封装在类中

var myPage = {}
myPage.foo = 1;
myPage.bar = 2;
myPage.doStuff = function(newFooValue) {
    this.foo = newFooValue;
}
你只需要在全球范围内使用一个点。 您应该避免这样做,但是如果您需要处理全局范围,那么您将这样做。 如果你问的是设计模式,你必须更加精确。
如果您有两个名为doStuff和domorseuff的函数,那么这是一件很难设计的事情。

谢谢,没有看到。谢谢,没有看到。谢谢您的解释。是的,我实际上想向页面上的其他函数公开foo和bar变量。为全局对象创建对象是否足以避免冲突?仅当包装对象没有命名冲突时。感谢您的解释。是的,我实际上想向页面上的其他函数公开foo和bar变量。为全局对象创建对象是否足以避免冲突?只有在包装对象没有命名冲突的情况下。谢谢,这是我提出的解决方案。我认为它避免了与其他全球公司的冲突。谢谢,这就是我提出的解决方案。我认为它避免了与其他全球卫星的碰撞。