What is the best way to store a value to use in a later function? I heard that global variables are evil

So the code I'm using is located at http://jsfiddle.net/8j947/10/ and returns true or false for the isLive variable. How can I use the onLive variable in a later function? I read the answers Accessing variables from other functions without using global variables , but I had problems working. All I want to do is save the value, true or false, so I can call it in an if statement in a later function. Can someone give me the easiest way to do this? Is there a way to save data as an object?

+4
source share
7 answers

Global variables are considered evil because any function can inadvertently change your variable, which can lead to some garbled errors. This is usually not a problem for simple projects, but it is something you should think about.

To preserve the value by not omitting the global namespace too much and thereby reducing the risk of the aforementioned errors, you can create your own β€œnamespace” object (option 2 of the accepted answer in your question). For instance:

var MyPageData = {}; MyPageData.someProperty = someFunc(); // Later on... function anotherFunc() { // Use the data you set... alert(MyPageData.someProperty); } 
+4
source

Just define your variable in the same area in which you define your functions that rely on it. If your functions are in global scope, then your variable should be.

If you are developing a plugin or control or script that will be used on multiple pages, then yes, avoid global variables. If, on the other hand, you have a piece of data that depends on the page that you need in more than one place, the global variable is absolutely suitable.

Go criticize! Vote for me! And when you're done with this, check out some of my other answers, where I really advocate using tables in HTML! :-)

Sometimes you need to understand the rules so that you know when it is normal to break them.

Now, if you just realized that your functions are in global scope, and you want to change this, just wrap all your code in an anonymous function and immediately call it:

 (function(){ var scopeLevelVariable = true; function scopeLevelFn(){ ... } window.globallyAvailableVariable = "foo"; window.globallyAvailableFunction = function(){ ... }; })(); 
+5
source

I would not agree with the fact that global variables are evil in themselves, it is just necessary to take several precautions when using them.

To avoid colliding with other global variables (perhaps written in the built-in libraries), I would suggest taking two measures

1) Use anonymous functions (self envoking) to keep the code (and variables) separate from other code

 // other peoples/modules code var myVariable = "whatever"; // anonymous function for your self contained code (function(){ var myVariable = "inside closure"; alert(myVariable); })() // the empty brackets envoke the anonymous function // still retains value from before your self envoking anonymous function alert(myVariable); 

2) Use a unique namespace - and make all the global variables that you use under this one object to avoid polling

 myUniqueNameSpaceRaiders = {}; // will not conflict with other global variables if your namespace is unique myUniqueNameSpaceRaiders.desiredVariable = "something"; 

I think that if you organize your global variables well, you can use them. Hope this helps.

+1
source

you should take a look at localStorage: http://diveintohtml5.ep.io/storage.html

0
source

If you work with a specific set of functions and data, you can do something like this:

 var yahooInterface = new function(){ var isLive = false; this.function1(){ isLive = true }; this.function2(){ alert( isLive ) }; }; 

Thus, function1 and function2 exchange data, but they do not wastefully pollute the global namespace.

Example: http://jsfiddle.net/XpJFn/1/

0
source

Try modular javascript programming.

In addition, closures are useful for storing information without publishing it to the global namespace.

If you must use the global ... create your own global namespace and put everything you need into it. This might be a good starting point for you as you risk using other javascript templates.

Ref.

 //Global Namespace window.ns = {}; ns.onLive = false; if(ns.onLive === false) { ns.notLive = !ns.onLive; } //Closures var hello = (function() { var onLive = true; my.fn1 = function () { return onLive; } my.fn2 = function () { return !onLive; } return my; })(); hello.fn1(); //true; hello.fn2(); //false 
0
source

If you are not using global variables, you can simply create a method that returns a variable.

 getVariable = function(){ return "Hello world!"; } 

Global variables are not inherently evil, though, when used properly. Just do not use them if you do not need it. If there is a good excuse for using a global variable, there is nothing wrong with that.

-1
source

All Articles