Wednesday, November 30, 2011

Local and Global Variables in AS3

In the example below, you will see two variable declarations and one function:

var outsideVar:String = "outside of the function";

function myFunction():void
{
     var insideVar:String = "inside of the function";
}

myFunction();

In the example above, we have a variable named outsideVar that's declared outside of the function, and another one named insideVar that's declared inside of the function.

Variables that are declared inside functions are referred to as local variables. So in this example, insideVar is a local variable. It is important to make this distinction because a local variable can be accessed only within the function that it was declared in.

Try to trace the local variable using a trace statement, but place this trace statement OUTSIDE of the function:

var outsideVar:String = "outside of the function";

function myFunction():void
{
     var insideVar:String = "inside of the function";
}

myFunction();

trace(insideVar);

Test the movie, and you will see that this will result in an error message that says: Access of undefined property. The undefined property being referred to in this case is the local variable that you are trying to access. It's being seen as undefined because the trace statement can't find the variable. This is because the variable is inside the function and is therefore local, while the statement trying to access it is outside of the function. Trying to access a local variable from outside the function will not work. Think of it this way: it's like the function is hiding the local variable from all the outsiders. Only the ones inside the function can see it. Everyone else can't.

So if we put the trace statement inside the function instead, we'll be able to trace the local variable when the function is called (remember to call the function, otherwise it won't run).

var outsideVar:String = "outside of the function";

function myFunction():void
{
     var insideVar:String = "inside of the function";
     trace(insideVar);
}

myFunction();

So now, if you test the movie, you'll be able to see the value of the local variable displayed in the output window.

On the other hand, a variable that's declared outside of a function can be accessed from inside a function as well as outside. For example, if we try to trace outsideVar using a trace statement inside the function, this will not give us any errors:

var outsideVar:String = "outside of the function";

function myFunction():void
{
     var insideVar:String = "inside of the function";
     trace(insideVar);
     trace(outsideVar);
}

myFunction();

If we try to trace outsideVar from outside of the function, we won't get any errors as well:

var outsideVar:String = "outside of the function";

function myFunction():void
{
     var insideVar:String = "inside of the function";
     trace(insideVar);
}

myFunction();

trace(outsideVar);

The variable named outsideVar, which we declared outside of the function, is referred to as a global variable - it is a variable that can be accessed in all areas of our code. We can access it from inside functions or outside of them.

Deciding between making a variable global or local
If you need the variable to be accessed in all areas of your code, then you can make it global by declaring it outside of any function. If the variable is only going to be used within a function's body and nowhere else, then make it local by declaring it inside the function that's going to use it.

1 comment:

  1. ACCESS PARENT VAR MovieClip(this.parent).variableName; ACCESS PARENT FUNC: MovieClip(this.parent).functionName;

    ReplyDelete