Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to store a value for use in a later function? I'm hearing global variables are evil

So the code I'm using is at http://jsfiddle.net/8j947/10/ and it returns a value of true or false for the variable isLive. How can I use the variable onLive in a later function? I read the answers at Accessing variables from other functions without using global variables, but I'm having a lot of trouble getting it to work. All I want is to store the value, true or false, so I can call it into an if statement in a later function. Can anyone give me the simplest way to do this? Is there a way to store the data as an object?

like image 562
davis Avatar asked Aug 11 '11 20:08

davis


People also ask

How do you store values in a variable?

To store a value in a variableUse the variable name on the left side of an assignment statement. The following example sets the value of the variable alpha . The value generated on the right side of the assignment statement is stored in the variable.

What should I use instead of global variables?

The Complete-Function Approach. Instead of using global variables to share data between a function and its caller, we can write functions so that they receive all the required information at the input and return all their results to the caller. That way, we provide all the inputs needed by the function to run.

How do you store a function in a variable?

Functions stored in variables do not need function names. They are always invoked (called) using the variable name. The function above ends with a semicolon because it is a part of an executable statement.

How do you store the same value in two variables?

You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.


2 Answers

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

If you are developing a plugin or control or a script that will be used in more than one page, then yes, avoid global variables. If, on the other hand, you have a page-specific piece of data that you need available in more than one location, a global variable is absolutely appropriate.

Go ahead critics! Down-vote me! And when your done with that check out some of my other answers where I actually advocate for using tables in HTML! :-)

Sometimes you have to understand the rules, so that you know when it's ok to break them.

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

(function(){
    var scopeLevelVariable = true;
    function scopeLevelFn(){
        ...
    }
    window.globallyAvailableVariable = "foo";
    window.globallyAvailableFunction = function(){
        ...
    };
})();
like image 142
gilly3 Avatar answered Sep 28 '22 05:09

gilly3


Global variables are considered "evil", because any function could inadvertently modify your variable, which can cause some hard-to-track bugs. This usually isn't a problem for simple projects, but it's something you should think about.

To save a value without muddying the global namespace too much, and thus reduce the risk of the aforementioned bugs, you can create you own "namespace" object (Option 2 of the accepted answer in the question you linked). Like so:

var MyPageData = {};

MyPageData.someProperty = someFunc();

// Later on...

function anotherFunc() {
    // Use the data you set...
    alert(MyPageData.someProperty);
}
like image 37
FishBasketGordo Avatar answered Sep 28 '22 07:09

FishBasketGordo