我听说过全球变量不好,我应该使用什么替代解决方案? [英] I've Heard Global Variables Are Bad, What Alternative Solution Should I Use?

查看:75
本文介绍了我听说过全球变量不好,我应该使用什么替代解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过全局变量不佳的地方,应该使用替代方法。在Javascript中,我应该选择什么解决方案。



我想到一个函数,当给两个参数( function globalVariables(Variable, Value))会查找变量是否存在于本地数组中,并且它是否将值设置为 Value ,否则,变量添加。如果函数被调用时没有参数(函数globalVariables()),它将返回数组。也许如果只用一个参数(函数globalVariables(Variable))触发该函数,它将返回变量中的值数组。



你怎么看?我希望听到您使用全局变量的替代解决方案和参数。



您将如何使用 globalVariables(); globalVariables(variable1,value1);

  // globalVariables()会将变量1附加到它的本地数组中。 
};

函数retrieve(){
var localVariable1 = globalVariables(variable1); // globalVariables()会返回value1。
};

函数retrieveAll(){
var localVariable1 = globalVariables(); // globalVariables()将返回globalVariable()的整个本地[永久存储在调用之间]数组。
};

函数set(){
globalVariables(variable1,value2); // globalVariables()会将variable1设置为value2。
};

这是

谢谢,感谢您的帮助!

全局变量在javascript中不鼓励的主要原因是,在javascript中,所有代码都共享一个全局命名空间, JavaScript暗含了全局变量,即。在本地作用域中未明确声明的变量会自动添加到全局名称空间中。依赖全局变量可能会导致同一页面上的各种脚本之间发生冲突(请阅读 douglas crockford的文章 )。



减少全局变量的一种方法是使用 YUI模块模式。基本思想是将所有代码包装在一个函数中,该函数返回一个包含需要在模块外部访问的函数的对象,并将返回值分配给单个全局变量。

  var FOO =(function(){
var my_var = 10; //共享变量仅在您的模块中可用

函数bar() {//这个函数在模块外不可用
alert(my_var); //这个函数可以访问my_var
}

return {
a_func:function() {
alert(my_var); //这个函数可以访问my_var
},
b_func:function(){
alert(my_var); //这个函数也可以访问my_var
}
};

})();

现在可以在其他地方的模块中使用函数,使用 FOO.a_func() 。通过这种方式来解决全局命名空间冲突,您只需更改 FOO 的名称。


I've read all over the place that global variables are bad and alternatives should be used. In Javascript specifically, what solution should I choose.

I'm thinking of a function, that when fed two arguments (function globalVariables(Variable,Value)) looks if Variable exists in a local array and if it does set it's value to Value, else, Variable and Value are appended. If the function is called without arguments (function globalVariables()) it returns the array. Perhaps if the function is fired with just one argument (function globalVariables(Variable)) it returns the value of Variable in the array.

What do you think? I'd like to hear your alternative solutions and arguments for using global variables.

How you would use globalVariables();

function append(){
    globalVariables("variable1","value1"); //globalVariables() would append variable1 to it's local array.
};

function retrieve(){
    var localVariable1 = globalVariables("variable1"); //globalVariables() would return "value1".
};

function retrieveAll(){
    var localVariable1 = globalVariables(); //globalVariables() would return the globalVariable()'s entire, local [persistently stored between calls] array.
};

function set(){
    globalVariables("variable1","value2"); //globalVariables() would set variable1 to "value2".
};

Is this a Singleton Pattern BTW?

In this specific scenario a function may set a variable at one point in time, and much later another function, maybe when a user submits a form, will need to get that variable. Therefore the first function couldn't pass the variable as an argument to the later function as it would never be called from the first.

Thank you, I appreciate all your help!

解决方案

The primary reason why global variables are discouraged in javascript is because, in javascript all code share a single global namespace, also javascript has implied global variables ie. variables which are not explicitly declared in local scope are automatically added to global namespace. Relying too much on global variables can result in collisions between various scripts on the same page (read douglas crockford's articles).

One way to reduce global variables is to use the YUI module pattern. The basic idea is to wrap all your code in a function that returns an object which contains functions that needs to be accessed outside your module and assign the return value to a single global variable.

var FOO = (function() {
    var my_var = 10; //shared variable available only inside your module

    function bar() { // this function not available outside your module
        alert(my_var); // this function can access my_var
    }

    return {
        a_func: function() {
            alert(my_var); // this function can access my_var
        },
        b_func: function() {
            alert(my_var); // this function can also access my_var
        }
    };

})();

now to use functions in your module elsewhere, use FOO.a_func(). This way to resolve global namespace conflicts you only need to change the name of FOO.

这篇关于我听说过全球变量不好,我应该使用什么替代解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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