
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
The Almighty Prototype Chain
|
311
A custom setSuperClass( ) function
If you decide to use __proto__ to establish inheritance (despite Macromedia’s recom-
mendations against it), consider creating a custom function that hides the slightly
obtuse
__proto__ syntax. Example 12-6 shows a custom setSuperClass( ) function
that assigns a superclass to a subclass (i.e., establishes inheritance) using
__proto__.
Note that we might have naturally used the name “extends” instead of “setSuper-
Class” (following Java terminology), but “extends” is reserved as a potential keyword.
A word of warning to
__proto__ users: when inheritance is established via
prototype.__proto__, Flash Player 6’s super operator cannot be used to invoke a
superclass constructor. The superclass constructor must be invoked with the follow-
ing older style syntax if inheritance was established using
__proto__:
function SubClass (someArg) {
this.base = SuperClass;
this.base(someArg);
delete this.base;
}
Example 12-6. A setSuperClass( ) function
// Add setSuperClass() to Function.prototype so it's
// available to all function objects
Function.prototype.setSuperClass = function (theSuperClass) {
// Establish the super/sub class relationship.
this.prototype.__proto__ = theSuperClass.prototype;
}
// Create a superclass
function Circle (radius) ...