Local Variables
Local variables are like stunt doubles for actors. They stand in to do a certain action, but don’t do anything else in the main movie script. When the function is finished doing its job, the local variable is finished doing his.
EX1:
function addStuff()
{
var i;
for( i = 0; i < 5 i++ )
{add some stuff}
}
addStuff();
‘i’ and ‘j’ are names given to local variables that are used for counting. Though you don’t have to necessarily use these two letters, they are standard.
Refer to the ‘Loops’ tutorial for more information on this.
So, the ‘i’ variable is only an actor for the ‘addStuff’ function.
Let’s fill it in:
EX2:
function addStuff():String
{
var i;
for( i = 0; i < 5; i++ )
{trace("hello")};
}
addStuff();
//hello
//hello
//hello
//hello
//hello
So, we start at ‘0’ and go one step at a time to ‘5’ and with each step we trace ‘hello.’
Let’s add one more thing to the above script:
var i:Number= 6
trace(i);
//6
Outside of the function, ‘i’ = 6, but the stunt double of ‘i’, who exists only in the 'addStuff' function, is used as a counter.
When I say that a local variable is a stunt double, I mean that it only exists inside of a function, not that it stands in for another variable, but that it is a temporary variable, used only by and for the function that it is in.
Local variables are not arguments. Remember that arguments are variables that can be switched out for other actors. If John is in the current scene(function) he can be switched out for Joe, as long as Joe is the understudy of John. What I mean is that if John is a number, then only numbers can play his role-- can be passed in in place of him.
However, local variables are created inside of the function and only exist inside of the function.
These are very similar ideas, but let’s look at an example:
for (var i:Number = 0; i < 3; i++)
{
trace("hello");
}
Now, this ‘for’ thing is a pre-built type of function, that we don’t have to worry about creating. It is a function that let’s you loop stuff. Remember that the for loop is like saying, “I want you to do something “for” some amount of time.
You can see that the ‘for’ loop has a local variable ‘i’ that sort of looks like an argument of the ‘for’ function.
In fact, arguments are local variables, but local variables are not always arguments.
From AobeFlex 3
-------
If the variable name you use for your local variable is already declared as a global variable, the local definition hides
(or shadows) the global definition while the local variable is in scope
To be passed by value means that the value of the argument is copied into a local variable for use within the function.
---------------------------
Resources for some information listed can be found in the Resource.pdf file.