PDA

View Full Version : global variable scope within function



ricmetal
04-12-2011, 01:34 PM
hiy
just a question
do global variables defined inside functions get destroyed when the function ends?

just to be sure i got this right, by global variable inside a function i mean this:

function myFunction() {
myVar = 500;
}

jscheuer1
04-12-2011, 03:39 PM
No. But that's an undeclared global variable that could lead to conflicts with other scripts, or with markup (the document object) in some browsers.

To avoid the first problem there are a number of things you can do. One would be to assign it as a property of the function:


function myFunction() {
myFunction.myVar = 500;
}

Then it would be available outside myFunction as:


myFunction.myVar

Another way would be to put the function itself inside another object (a function or a simple javascript object). That gets a little complicated and has various ways it can be done depending upon personal choice and the code objectives. If you want to learn about that, give a more concrete example.

Both of the above approaches will also solve the second issue, which can be avoided without them (assuming this script runs on a page in a window as almost all javascript does) by declaring it as a property of the window object (the window is the global object in that case):


function myFunction() {
window.myVar = 500;
}

But just in case it doesn't, you can simply declare it outside the function:


var myVar;
function myFunction() {
myVar = 500;
}

This latter is probably better than the window.myVar method because it will avoid some script errors in cases where myVar gets accessed before myFunction has run.

traq
04-12-2011, 07:59 PM
John - a related question:

I was under the impression that declaring a variable, using the var keyword (construct, whatever it is) would keep it local:

function myFunc(){
var myVar = 500 /* local variable */
myVar2 = 600 /* global variable */
}
is this correct?

ricmetal
04-12-2011, 11:44 PM
thanks jscheuer1

jscheuer1
04-13-2011, 12:33 AM
John - a related question:

I was under the impression that declaring a variable, using the var keyword (construct, whatever it is) would keep it local:

function myFunc(){
var myVar = 500 /* local variable */
myVar2 = 600 /* global variable */
}
is this correct?

Yes. The same caveats previously mentioned apply to the global there. It's an undeclared global variable. In some IE (all perhaps), if you also have:


<div id="myVar2">Whatever</div>

There will most likely be conflicts. These can be avoided by formally declaring the variable as global, also as previously mentioned.

Extra Stuff:

One thing I didn't make clear though is, if you use the window.myVar2 method:


function myFunc(){
var myVar = 500 /* local variable */
window.myVar2 = 600 /* global variable */
}

Then myVar2 is available outside the function as myVar:


function myFunc(){
var myVar = 500 /* local variable */
window.myVar2 = 600 /* global variable */
}
myFunc();
alert(myVar2); /* alerts 600 */

But, as I also mentioned, declaring it before hand is preferable:


var myVar2;
function myFunc(){
var myVar = 500 /* local variable */
myVar2 = 600 /* global variable */
}
myFunc();
alert(myVar2); /* alerts 600 */

That also avoids a conflict with the document object, and avoids an error in cases like so:


var myVar2;
function myFunc(){
var myVar = 500 /* local variable */
myVar2 = 600 /* global variable */
}
alert(myVar2); /* alerts 'undefined' */

Whereas with the window.myVar2 approach:


function myFunc(){
var myVar = 500 /* local variable */
window.myVar2 = 600 /* global variable */
}
alert(myVar2); /* throws an error */

Extra Extra Stuff:

All of this is interesting and can contribute to one's understanding in working with older code, and by extension to an understanding of scope in any code. For new code though, it's best to avoid situations like these in the first place by making all variables except possibly one local to some scope:


function SomeConstructor(params){
whatever . . .
}

Invoked:


new SomeConstructor(params);

The SomeConstructor function can be global, but all subsequent functions and values can be prototype functions and values of the constructor, or objects of it.

If all interactive events are assigned by SomeConstructor and it's prototypes and objects - SomeConstructor, it's prototypes and objects may be declared within an anonymous function, exposing nothing to the global scope.

ricmetal
04-13-2011, 07:09 PM
good informative post
thanks very much for it.
i actually got what i needed to now as soon as you said that the variable was undefined and could lead to conflicts with the rest of the script. that is what was happening.
i used the approach where i defined the var outside the function, and the code works now.
thanks again
best regards