简体   繁体   中英

Javascript Global Variables

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

so something like:

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

EDIT

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

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.

Here's a simple example:

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");

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.


The point is to stay away from defining variables in the global namespace (ie, 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"

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.

Call the function as globVar('myVar','myVarValue')

function globVar(variable,value){
   window[variable] = value;
}

Here is a jsfiddle: http://jsfiddle.net/emQQm/

window.variable = variable;

Don't understand what is an "automatic mode", so this might not what you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM