| |
|
||||
|
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.
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.
Instances < < Home > > Inheritance
|