O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Prototype Chain

In the preceding sections, we learned that a prototype object can be used to share information and behavior with the objects created from a particular constructor function or class. In fact, the reach of a given prototype object goes beyond the objects created from the function or class to which it is attached.

In the case of a class, the dynamic instance variables defined on the class's prototype object can be accessed not just through the class's instances but also through the instances of the class's descendants. The following generic code demonstrates:

// Create a simple class, A
dynamic public class A {
}

// Create another simple class, B, that extends A
dynamic public class B extends A {
}

// Create an application's main class
public class Main extends Sprite {
  public function Main () {
    // Add a dynamic instance variable to class A's prototype object
    A.prototype.day = "Monday";

    // Access A.prototype.day through an instance of B
    var b:B = new B();
    trace(b.day);  // Displays: "Monday"
  }
}

In the case of a function, the dynamic instance variables defined on the function's prototype object can be accessed not just through any object created from that function but also through any object whose prototype chain includes that function's prototype object.

Let's explore how prototype chains work through an example. Suppose we create a function, Employee( ), just as we did earlier, whose prototype object has a dynamic instance variable named company:

function Employee () ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required