User Guide
Advanced topics 145
The prototype chain, which was the only inheritance mechanism in previous versions of
ActionScript, serves only a secondary role in ActionScript 3.0. The primary inheritance
mechanism, fixed property inheritance, is handled internally by the traits object. A fixed
property is a variable or method that is defined as part of a class definition. Fixed property
inheritance is also called class inheritance because it is the inheritance mechanism that is
associated with keywords such as
class, extends, and override.
The prototype chain provides an alternative inheritance mechanism that is more dynamic
than fixed property inheritance. You can add properties to a class’s prototype object not only
as part of the class definition, but also at run time through the class object’s
prototype
property. Note, however, that if you set the compiler to strict mode, you may not be able to
access properties added to a prototype object unless you declare a class with the
dynamic
keyword.
A good example of a class with several properties attached to the prototype object is the
Object class. The Object class’s
toString() and valueOf() methods are actually functions
assigned to properties of the Object class’s prototype object. The following is an example of
how the declaration of these methods could, in theory, look (the actual implementation
differs slightly because of implementation details):
public dynamic class Object
{
prototype.toString = function()
{
// statements
};
prototype.valueOf = function()
{
// statements
};
}
As mentioned previously, you can attach a property to a class’s prototype object outside the
class definition. For example, the
toString() method can also be defined outside the Object
class definition, as follows:
Object.prototype.toString = function()
{
// statements
};