Inheritance All-Rounder v1.08


== オブジェクトとムービークリップ、いずれにも使用可能な汎用継承メソッド ==

Download the source file
(含使用例)



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

/*
objにスーパークラスのインスタンスプロパティを継承
*/
Object.inheritProps = function (obj, superClass, oArgs) {
        obj.temp = superClass;
        obj.temp(oArgs);
        delete obj.temp;
}

/*
"sub"のprotoチェーンの中の最後の__proto__をsuperclass.prototypeに置換。
引数"sub"はクラス、又はクラスから作られたインスタンス
*/
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;
}

/*
次の"inherit"メソッドは"this"をあたかもsuperClassのインスタンスであるかの如くセット。
もし"this"が既に他のクラスから継承している場合は"this"の最後の__proto__(Object.prototypeへの参照)
をsuperClass.prototype(これが他から継承している場合も可)に置換。

ムービークリップにも適用可。
ムービークリップの場合はそのムービークリップの最初の__proto__をsuperClass.prototype
(これが他から継承している場合も可)に置換。
*/
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 hidden methods diggers ;)
////////////////////////////////////////////////////////////////////////////////////////////////

Back to Flash Home

©2000-2002 STUDIO FIRST RAYS. All rights reserved