Inheritance All-Rounder v1.08


== Generic Inheritance Methods For Both Objects And Movieclips ==

Download the source file
(Usage Examples included)

 

////////////////////////////////// Inheritance All-Rounder 1.08 ////////////////////////////////
//                                                                  Jan.24.2002 Tatsuo Kato
/////////////////////////////////////////////////////////////////////////////////////////////////

/*
Make obj inherit instance properties from superClass.
*/
Object.inheritProps = function (obj, superClass, oArgs) {
        obj.temp = superClass;
        obj.temp(oArgs);
        delete obj.temp;
}

/*
Replace the last __proto__ of sub's proto chain with superClass' prototype.
Here, the argument "sub" can be either a class or an instance created from the class.
*/
Object.inheritPrototype = function (sub, superClass) {
        if (typeof sub == "function") {
                var o = sub.prototype;
        } else if (typeof sub == "object") {
                var o = sub;
        } else {
                return false;
        }
        while (o.__proto__.__proto__ != null) {
                o = o.__proto__;
        }
        o.__proto__ = superClass.prototype;
}

/*
The following 'inherit' method sets 'this'(the caller) as an instance of 'superClass'.
And if 'this' is already inheritting from other classes, its last __proto__,
which should be refering to Object.prototype, will be replaced with 
superClass' prototype which may also be inheritting from others.

This works on a movieclip as well.
(i.e. The caller can be a movieclip as well.)
And if it's a movieclip, the movieclip's first __proto__ will be replaced 
with superClass' prototype which may also be inheritting from others.
*/
Object.prototype.inherit = function (superClass, oArgs) {
        if (typeof this == "object") {
                Object.inheritPrototype(this, superClass);
        } else if (typeof this == "movieclip") {
                if (!new superClass().duplicateMovieClip) {
                        Object.inheritPrototype(superClass, MovieClip);
                }
                this.__proto__ = superClass.prototype;
        }
        Object.inheritProps(this, superClass, oArgs);
}

ASSetPropFlags(Object.prototype, ["inherit"], 1); //Thanks to the hidden methods diggers ;)
////////////////////////////////////////////////////////////////////////////////////////////////

Back to Flash Home

Copyright©2000-2002 STUDIO FIRST RAYS. All rights reserved