オブジェクトの継承2

概要

ベースオブジェクトのコンストラクタを呼び出して処理を行うことが可能

※ちょっと加工が必要なので大変!

定義

// 継承制御
function initializeBase(device, base, baseArgs){
    // 親クラスのコンストラクタを呼び出す
    base.apply(device, baseArgs);
 
    // プロパティオブジェクトをコピー
    for(prop in base.prototype){
        var proto = device.constructor.prototype;
        if(!proto[prop]){
            proto[prop] = base.prototype[prop];
        }
    }
}
 
// ベースオブジェクト定義
var MyBaseClass = function(name, count){
    // コンストラクタ
};
 
// オブジェクトリテラルでプロトタイプを定義
MyBaseClass.prototype ={
    // プロトタイプオブジェクト
};
 
// ベースオブジェクト定義
var MyClass = function(パラメータ){
    // 既定オブジェクトのコンストラクタを実行
    initializeBase(this, ベースオブジェクト, [パラメータ]);
 
    // メンバを設定
 
}
 
 

サンプル(オブジェクト階層が2階層)

ソース

// 継承制御
function initializeBase(device, base, baseArgs){
    // 親クラスのコンストラクタを呼び出す
    base.apply(device, baseArgs);
 
    // プロパティオブジェクトをコピー
    for(prop in base.prototype){
        var proto = device.constructor.prototype;
        if(!proto[prop]){
            proto[prop] = base.prototype[prop];
        }
    }
}
 
// ベースオブジェクト定義
var MyBaseClass = function(name, count){
    // メンバ変数設定
    this.name  = name;
    this.count = count;
};
 
// オブジェクトリテラルでプロトタイプを定義
MyBaseClass.prototype ={
    getData01 : function(){
                    return 'name  = ' + this.name + '<br />';
                },
    getData02 : function(label){
                    return 'count = ' + this.count + '<br />';
                },
};
 
// ベースオブジェクト定義
var オブジェクト = function(name, count, label){
    // 既定オブジェクトのコンストラクタを実行
    initializeBase(this, MyBaseClass, [name, count]);
 
    // メンバを設定
    this.label = label;
 
}
 
// 継承されたオブジェクトにメソッドを追加する場合は個別に設定する
MyClass.prototype.getData03 = function(){
    return 'label = ' + this.label + '<br />';
};
 
// オブジェクトのインスタンスを生成して実行
var obj1 = new MyClass('User01', 1000, 'Test');
 
// MyBaseClassのメソッド結果
document.writeln(obj1.getData01());
document.writeln('<br />');
 
// MyBaseClassのメソッド結果
document.writeln(obj1.getData02());
document.writeln('<br />');
 
// MyClassのメソッド結果
document.writeln(obj1.getData03());
document.writeln('<br />');
 
 

結果

name = User01
 
count = 1000
 
label = Test
 
 

サンプル(オブジェクト階層が3階層)

ソース

// 継承制御
function initializeBase(device, base, baseArgs){
    // 親クラスのコンストラクタを呼び出す
    base.apply(device, baseArgs);
 
    // プロパティオブジェクトをコピー
    for(prop in base.prototype){
        var proto = device.constructor.prototype;
        if(!proto[prop]){
            proto[prop] = base.prototype[prop];
        }
    }
}
 
// ベースオブジェクト定義
var MyBaseClass = function(name, count){
    // メンバ変数設定
    this.name  = name;
    this.count = count;
};
 
// オブジェクトリテラルでプロトタイプを定義
MyBaseClass.prototype ={
    getData01 : function(){
                    return 'name  = ' + this.name + '<br />';
                },
    getData02 : function(label){
                    return 'count = ' + this.count + '<br />';
                },
};
 
// ベースオブジェクト定義
var MyClass = function(name, count, label){
    // 既定オブジェクトのコンストラクタを実行
    initializeBase(this, MyBaseClass, [name, count]);
 
    // メンバを設定
    this.label = label;
 
}
 
// 継承されたオブジェクトにメソッドを追加する場合は個別に設定する
MyClass.prototype.getData03 = function(){
    return 'label = ' + this.label + '<br />';
};
 
 
// ベースオブジェクト定義
var MyClass2 = function(name, count, label, option){
    // 既定オブジェクトのコンストラクタを実行
    initializeBase(this, MyClass, [name, count, label]);
 
    // メンバを設定
    this.option = option;
 
}
MyClass2.prototype.getData04 = function(){
    return 'option = ' + this.option + '<br />';
};
 
 
// オブジェクトのインスタンスを生成して実行
var obj1 = new MyClass2('User02', 3000, 'Test', 'Option');
 
// MyBaseClassのメソッド結果
document.writeln(obj1.getData01());
document.writeln('<br />');
 
// MyBaseClassのメソッド結果
document.writeln(obj1.getData02());
document.writeln('<br />');
 
// MyClassのメソッド結果
document.writeln(obj1.getData03());
document.writeln('<br />');
 
// MyClass2のメソッド結果
document.writeln(obj1.getData04());
document.writeln('<br />');
 
 

結果

name = User02
 
count = 3000
 
label = Test
 
option = Option
 
 


最終更新:2012年02月18日 10:51