Introduction
Objects
Instances
Local
Inheritance
Prototypes
Override
Overwrite
Protection
Arguments
Constructors
Methods
Proto
Arguments Array
Callee
New
Extend
Super
Glossary
 
 
Debreuil Digital Works © 2001
 

Local properties inside a function

What happens if we define a property using the var keyword? A local property is created. Local simply means 'local to the scope of the current object', or perhaps, 'in this container' if you prefer. Kind of like the 'local yokels' in a small town - they are born, raised, and buried in the same small town, and never manage to get out. Here is an example of a local property defined in a class...

A = function()
{
   var temp = 5;
   this.x   = temp; 
}
trace(A.temp); // not here

Where exactly does the property temp live in this example? It is in the 'activation object' (sometimes also called the 'constructor function') portion of function A. Does this mean it could be found by saying A.temp? Well, no, because it is only being defined in this example - the whole activation object section doesn't run at all until A( ) is called. Does this mean it would be available after A is run? No again, because that object is discarded once A is run. In fact, every time a function is run, this activation object is created, and every time it is finished running, the activation object is discarded (though not usually deleted from memory in Flash 5, tsk tsk). So temp lives inside the scope of this temporary object. However, because the activation object has no name, and it is only around when the function is running, it cannot be accessed by any other part of the program (unless you call a function from a function, then the second function can see it with its "this" property). When it finishes running, its local properites are not saved.

People often say that in actionscript 'everything is an object', which is not really right because some things clearly aren't objects, and strangely, some things are more than one object. The function above is two objects (actually it's three, but we'll get to that). There is the reference object called A that behaves like any other object. You can add properties to it, like A.temp, and those properties will be put into the A object. Then you have the block of instructions that make up the function. This is will be the second object, the activation object, that is created every time a function is called.

The usefulness of the first A object is limited. Its biggest job is giving the function block a name so that it can be called. After that you can use its namespace for information about the function itself. An example of such information would be it's classname (for debugging purposes), or a counter that counts the number of instances that have been created from it. Properties in this location will have no effect on running the function, and will not show up in its instances.

The second object, the activaton object, is where the 'building' happens while creating an instance, so it is quite useful of course. It has its own scope, and its "this" keyword is given the box on which it can create or modify properties. This is only part of the story when creating an instance though - perhaps the famous part, but not necessarily the major part as we will see when we dig into the prototype.

The idea of functions having a block of instructions that run when you call them relates very well to Flash. The symbol in the library has information that tells instances what they will look like and how they will behave. What about the other part of the function though, like the 'A object' in the previous example? This location is used for storing information that tells about the object, but is not relevant to the instances. For example all the symbols in the library have a name, but this name isn't given (or relevant) to the instances. If you check 'Update Use Count' in the library, each symbol shows how many times it has been used in your movie. Again, this is a property that is relevant to the class, but not the instances, therefore the logical place for this kind of information is attached to the class object. In actionscript, you can get access to this information (using intance.constructor.temp, but more on that later) where as in Flash it is not available to your instances (only to you, the user).

Though you don't often have a need for this namespace while programming in Actionscript, it sometimes is the perfect place. An example of this would be counter that keeps track of how many instances have been created - much like "Use Count" does in Flash.

 

Instances < < Home > > Inheritance