Javascript全局变量 [英] Javascript Global Variables

查看:96
本文介绍了Javascript全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何创建一个变化的变量作为全局变量?

How should I create a changing variable as a global variable?

所以类似于:

function globVar(variable){
   window.variable;
}

所以这样我也可以在自动模式下创建全局变量,并且我也可以更轻松地创建它们:)

So in this way I could create global variables in an automatic mode too, and also I could create them for myself easier :)

编辑

例如我可以像这样创建一个全局变量: globVar('myVariable'); 然后 myVariable 被添加到全局变量中变量。

For example I could create a global variable just like this: globVar('myVariable'); then myVariable is added to the global variables.

推荐答案

很抱歉这样说,但你收到的答案是你应该远离的坏习惯。 更好的编程习惯,也许正确的编程实践,将伪命名空间全局变量,以免混淆全局命名空间/范围。这背后的原因是为了使您的代码更易于管理,更重要的是,如果/当您的应用程序变大时,让您的生活更轻松。定义命名空间的一种简单机制是使用由Douglas Crockford着名的模块模式。

Sorry to say this, but the answers you have received are bad habits that you should stay away from. A better programming practice, and perhaps the proper programming practice, would be to pseudo-namespace your global variables so to not clutter the Global namespace/scope. The reasoning behind this is to make your code more manageable, and more importantly, make life easier for you if/when your application becomes large. A simple mechanism to define a namespace is to use the module-pattern made famous by Douglas Crockford.

这是一个简单的例子:

var myNamespace = function(){
  var o = {};
  var globals = {};

  var setGlobVar = function(name, value) {
    globals[name] = value;
  };

  var getGlobVar = function(name) {
    if (globals.hasOwnProperty(name)) {
      return globals[name];
    } else {
      // return null by default if the property does not exist 
      return null;
    }
  };

  o.setGlobVar = setGlobVar;
  o.getGlobVar = getGlobVar;
  return o;
}();

要使用它,您只需将其称为对象的方法。

To use this, you simply call it like methods of an object.

myNamespace.setGlobVar("secret_msg", "Dumbledore dies, so does Hedwig");
myNamespace.getGlobVar("secret_msg");

你也可以公开 globals 变量如果你想简化访问变量的方法,可以使用 setGlobVar getGlobVar 方法来使用它。

You could also expose the globals variable instead of using the setGlobVar and getGlobVar methods to use it, if you wanted to simplify how you access the variable.

重点是远离在全局命名空间中定义变量(即窗口 object)尽可能通过创建自己的命名空间。这样可以减少名称冲突,意外重写或覆盖的可能性,以及全局命名空间的混乱。

The point is to stay away from defining variables in the global namespace (i.e., the window object) as much as possible, by creating a namespace of your own. This reduces the chance of name collisions, accidentally rewrites or overrides, and again, global namespace clutter.

更简单的方法是简单地定义一个对象并扩充其属性。

An even simpler approach to doing this is to simply define an object and augment its properties.

var globals = {};
globals.SECRET_MSG = "Snape is not a traitor"

虽然我会扩展这种方法通过将 globals 包装到特定于我的应用程序的命名空间中。

Though I would extend this approach by wrapping globals into a namespace that is specific to my application.

var myNamespace = {};
myNamespace.globals = {};
myNamespace.globals.SECRET_MSG = "Snape is not a traitor"

注意:这是实际上与我建议的原始模块模式方法相同,只是没有获取设置访问器方法并且编码方式不同。

NOTE: This is actually the same as original module-pattern approach I suggested, just without the get and set accessor methods and coded differently.

这篇关于Javascript全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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