Variable-to-Property Analogy

 <  Day Day Up  >  

In Chapter 2, "All the World Is an Object," the text looked at a class called Loan . The Loan class had several properties that described aspects of a loan, such as principal , rate , and term . These properties acted much like a variable within the loan instances. In reality, that's exactly what a property is: a variable belonging to an object. Properties can be used in exactly the same ways as a variable. That is, they can be passed to functions, compared with conditionals, and so on.

In reality, what we know of as a variable is actually a property; the movie clip or timeline on which it is coded is the object to which it belongs. Consider this example: In Frame 1 of the Actions layer on the main timeline, this code is entered:

 var myName:String = "jeff"; 


Just like we saw with other object types, adding a strong data type to the String declaration enables the code hinting for strings.

Logically, we recognize that this is creating a variable, called myName , and assigning it a value of " jeff ". In reality, as it is coded on the timeline of a movie clip, it is actually a property of that movie clip.

This can be demonstrated by tracing it in several different ways:

 trace(myName); trace(this.myName); trace(_root.myName); 

Each elicits exactly the same response. What this shows is that a simple variable within a movie clip can be referred to as a variable (shown with the first trace statement) or as a property of an object (shown in the second and third trace statements).

To refer to the variable as a property, it needs to be referenced using the notation. It is a property of a movie clip on whose timeline it is coded, which we can refer to by its relative address this or its absolute address _root .

 <  Day Day Up  >  

Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162 © 2008-2017.
If you may any questions please contact us: