For example, the following code creates a simple class named PrivateExample with one private
variable, and then attempts to access the private variable from outside the class. In
ActionScript 2.0, compile-time access was prohibited, but the prohibition was easily
circumvented by using the property access operator (
run time rather than at compile time.
class PrivateExample
{
private var privVar:String = "private variable";
}
var myExample:PrivateExample = new PrivateExample();
trace(myExample.privVar);
trace(myExample["privVar"]); // ActionScript 2.0 allows access, but in
ActionScript 3.0, this is a run-time error.
In ActionScript 3.0, an attempt to access a private property using the dot operator
(
myExample.privVar
error is reported at run time, just as it is when you use the property access operator
(
myExample["privVar"]
The following table summarizes the results of attempting to access a private property that
belongs to a sealed (not dynamic) class:
dot operator (.)
bracket operator ([])
In classes declared with the
result in a run-time error. Instead, the variable is simply not visible, so Flash Player returns the
value
. A compile-time error occurs, however, if you use the dot operator in strict
undefined
mode. The following example is the same as the previous example, except that the
PrivateExample class is declared as a dynamic class:
dynamic class PrivateExample
{
private var privVar:String = "private variable";
}
var myExample:PrivateExample = new PrivateExample();
trace(myExample.privVar);
trace(myExample["privVar"]); // output: undefined
112
Object-Oriented Programming in ActionScript
// compile-time error in strict mode
) is a compile-time error if you are using strict mode. Otherwise, the
).
Strict mode
compile-time error
run-time error
attribute, attempts to access a private variable will not
dynamic
// compile-time error in strict mode
), which does the property lookup at
[]
Standard mode
run-time error
run-time error