User Guide

112 Object-Oriented Programming in ActionScript
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 (
[]), which does the property lookup at
run time rather than at compile time.
class PrivateExample
{
private var privVar:String = "private variable";
}
var myExample:PrivateExample = new PrivateExample();
trace(myExample.privVar); // compile-time error in strict mode
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) is a compile-time error if you are using strict mode. Otherwise, the
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:
In classes declared with the
dynamic attribute, attempts to access a private variable will not
result in a run-time error. Instead, the variable is simply not visible, so Flash Player returns the
value
undefined. A compile-time error occurs, however, if you use the dot operator in strict
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); // compile-time error in strict mode
trace(myExample["privVar"]); // output: undefined
Strict mode Standard mode
dot operator (.) compile-time error run-time error
bracket operator ([]) run-time error run-time error