Make your own free website on

About "var"

What is var:

Lets see this script:

enableButton = function () {
    var id = i;
    _root["btn"+id].onPress = function() {
for (i=0; i<5; i++) {

In the function "enableButton", we declared a variable "id" as var; Traditionaly, we say it is "temporary" variable. It is created when the function is called and "expires" when the function ends its process. 

Flash documentation does not use the word "temporary". Instead, it is said to be "local". Documentation still uses the word "expire", however, it is not necessarily true; The var id in the example above survives and lives there and accessed by button onPress function.

Allocate memory for var and function object

First I explain the job that is done by the script above.

Through a for-loop, we call the function "enableButton" 5 times. The first calling, we meet a variable declared as "var". System allocate a new memory piece for the variable "id". Lets say, it is at the address of 1000; It named the address as variable name "id" and assign the value i which is 0 to the memory piece. Then the process allocate a memory block to create a function object and return the handle to _root.btn0.onPress; Lets say, the function object is created at address 1001;

Here we see two memory blocks are created. One is for variable "id", and the other is for function object. Please note that, the function object is created here not in the button. What the button.onPress stored is just a reference to this memory block, say 1001; If at later time, we say btn0.onPress=otherFunction; It changes what onPress stores, not the function object created here.

The second calling of enableButton with i=1 will do the same thing. System allocate another memory block, say 2000 to store a variable name as "id" , assigns the value 1 to it. Then it allocate new memory block for function object. Lets say the address is 2001;

Now we have in memory: 1000 for id, 1001 for function object, 2000 for id, 2001 for function object. The third call, forth call and the fifth call will add the same thing to memory block.

How to resolve the variable

Now, lets see what will happen when button is clicked.

When the button btn0 is clicked, system search for btn0.onPress. There it stores the reference of a function object. So, system trace to the memory address of 1001 and execute that function. When that function is executed, system allocate memories for any "var" variable needed. In our case, there is only one line of script "trace(id);" not any "var" is declared. To trace the variable "id", it search for the memory that stores the variables and there is nothing. So, it goes up back to the address 1001 and OK, in that set of memory block, there is memory piece at 1000 named as "id", so it trace out the value stored in that memory and output "0";

That is how Flash do jobs.

So. we see that we called that function 5 times and thus created 5 version of variable id. All of them persist in the memory and do not "expire".

However, even they lives there how do we access those variables ? Those 5 version of "id" are not properties of timeline. Nor they belong to the function object of "enableButton". We can not access those id by nor can we access them by; The one that can access those id is the function object created at the same time. The function object at 2001 can access the variable at 2000.

So, some would use this feature to implement addProperty. We declare a variable as "var" and then create getter and setter function object at the same time accessing this var variable. Since only these getter and setter function object can access the variable, this variable turns to be "private".The only way to access it is through getter/setter function. No other way to access that variable 

Most of the time, a function just create many var variables and does not create any function object like the example above. In such condition, those variables in the memory block will be impossible to access them any more. So, they may well be cleared from the memory. That is - the "expire". 

Also we know that, when System want to resolve the variable "id", it does not "remember" or "record" the memory address of that variable. It just trace along a route to find a variable named as "id" until it is found.

Arguments of a function:

When we script a function with parameters: 


The "id" in the argument list behaves like "var id"; When this function is called, system also allocate a memory and name the variable as "id"; Then every thing is the same as described above.

Var in frame script

Frame script is different from "function" in many aspect. Anyway, it is not a "function". All variables in frame script is taken as "properties" of that timeline. A variable "id" declared in _root, We can access that variable by; When we declare a variable as "var", system does not allocate memory outside of the timeline object. So, "var" does not make sense in frame script. It make sense only in function.

Declare a variable as var in later time

    var id;
    var id;
    delete id;

Lets check this script and see how the jobs are done.

First me meet a line : "id=4;". There is no yet any allocated memory for var, so it trace up to timeline and found variable "id" in the timeline to be 3; So, the output is "3".

Then we meet "var id;". Here system allocate a memory piece and name this variable as "id". It is null yet. Note that, it is null but strictly not undefined. It is already defined as null.

Next is "id=5;". Now system finds a variable name "id" in the allocated memory, so it will not go up further to timeline. It assign the value "5" to the memory piece. Of-course, following this, the trace will output "5";

The interesting scripts start from here.

We meet "var id;" again. We might expect system allocate a memory piece for variable "id". But, there is already a variable "id" in the memory. Will it create a new one and reset "id" to null ? No ! It does nothing. So, the old var id still exists there and the trace will outpu "5";

Now I want to delete the variable id in the allocated memory, so I can access the id of timeline. Can I ? Well you can delete the id of timeline. You even can delete the variable "ab" which stores the reference of this function. However, the id in the allocated memory can not be deleted before the end of this function. So, the trace still output "5";