Class property attributes



In discussions of the ActionScript object model, the term property means anything that can be a member of a class, including variables, constants, and methods. This differs from the way the term is used in the ActionScript 3.0 Language and Components Reference, where the term is used more narrowly and includes only class members that are variables or are defined by a getter or setter method. In ActionScript 3.0, there is a set of attributes that can be used with any property of a class. The following table lists this set of attributes.

Attribute

Definition

internal (default)

Visible to references inside the same package.

private

Visible to references in the same class.

protected

Visible to references in the same class and derived classes.

public

Visible to references everywhere.

static

Specifies that a property belongs to the class, as opposed to instances of the class.

UserDefinedNamespace

Custom namespace name defined by user.

Access control namespace attributes

ActionScript 3.0 provides four special attributes that control access to properties defined inside a class: public, private, protected, and internal.

The public attribute makes a property visible anywhere in your script. For example, to make a method available to code outside its package, you must declare the method with the public attribute. This is true for any property, whether it is declared using the var, const, or function keywords.

The private attribute makes a property visible only to callers within the property’s defining class. This behavior differs from that of the private attribute in ActionScript 2.0, which allowed a subclass to access a private property in a superclass. Another significant change in behavior has to do with run-time access. In ActionScript 2.0, the private keyword prohibited access only at compile time and was easily circumvented at run time. In ActionScript 3.0, this is no longer true. Properties that are marked as private are unavailable at both compile time and run time.

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) results in 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:

 

Strict mode

Standard mode

dot operator (.)

compile-time error

run-time error

bracket operator ([])

run-time error

run-time error

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 or Adobe® AIR™ 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

Dynamic classes generally return the value undefined instead of generating an error when code external to a class attempts to access a private property. The following table shows that an error is generated only when the dot operator is used to access a private property in strict mode:

 

Strict mode

Standard mode

dot operator (.)

compile-time error

undefined

bracket operator ([])

undefined

undefined

The protected attribute, which is new for ActionScript 3.0, makes a property visible to callers within its own class or in a subclass. In other words, a protected property is available within its own class or to classes that lie anywhere below it in the inheritance hierarchy. This is true whether the subclass is in the same package or in a different package.

For those familiar with ActionScript 2.0, this functionality is similar to the private attribute in ActionScript 2.0. The ActionScript 3.0 protected attribute is also similar to the protected attribute in Java, but differs in that the Java version also permits access to callers within the same package. The protected attribute is useful when you have a variable or method that your subclasses need but that you want to hide from code that is outside the inheritance chain.

The internal attribute, which is new for ActionScript 3.0, makes a property visible to callers within its own package. This is the default attribute for code inside a package, and it applies to any property that does not have any of the following attributes:

  • public

  • private

  • protected

  • a user-defined namespace

The internal attribute is similar to the default access control in Java, although in Java there is no explicit name for this level of access, and it can be achieved only through the omission of any other access modifier. The internal attribute is available in ActionScript 3.0 to give you the option of explicitly signifying your intent to make a property visible only to callers within its own package.

static attribute

The static attribute, which can be used with properties declared with the var, const, or function keywords, allows you to attach a property to the class rather than to instances of the class. Code external to the class must call static properties by using the class name instead of an instance name.

Static properties are not inherited by subclasses, but the properties are part of a subclass’s scope chain. This means that within the body of a subclass, a static variable or method can be used without referencing the class in which it was defined. For more information, see Static properties not inherited.

User-defined namespace attributes

As an alternative to the predefined access control attributes, you can create a custom namespace for use as an attribute. Only one namespace attribute can be used per definition, and you cannot use a namespace attribute in combination with any of the access control attributes (public, private, protected, internal). For more information about using namespaces, see Namespaces.