
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
302
|
Chapter 12: Objects and Classes
Object literals are used for the sake of terseness. For example, in the following code
we transfer the
speed and onEnterFrame properties to car_mc more succinctly (though
perhaps less legibly) than we did earlier:
this.attachMovie("CarSymbol", "car_mc", 1, {speed:3, onEnterFrame: function ( ) {
this._x += this.speed;}});
The Almighty Prototype Chain
In Chapter 2, we saw that the interpreter uses a scope chain to resolve (look up the
value of) unqualified variable references. To resolve property values, the interpreter
uses a prototype chain. We’ve already encountered the
prototype property in our
examination of methods and superclasses. Now let’s take a look “under the hood” of
ActionScript’s inheritance system to see how the
prototype property really works.
For the sake of the current discussion, the term property refers to object properties,
methods, and event handlers (the latter two of which are, technically, functions
stored in properties).
As we’ve seen, every class constructor function is automatically given a property
called
prototype. For example, when we create a Book class, the Book constructor
automatically defines
Book.prototype:
function Book ( ) {
}
trace(Book.prototype); // Displays: [object Object]
Initially, a constructor function’s prototype ...