3

2 つのクラスがあるClassAとしClassBます。

function ClassA(){
    this.x = 1;
    this.y = 'a';
    this.z = false;
}

-

function ClassB(){
    this.x = 0;
    this.y = 'b';
    this.z = true;
}

そして、プロトタイプとしてこれら 2 つに対して 1 つの関数を使用したいと思います。

var foo = function(){
    if(this.z){
         window.alert(this.y + this.x);
    }
}

のように使用しても安全ですか

ClassA.prototype.foo = foo;

ClassB.prototype.foo = foo;

異なるクラスに同じ関数オブジェクトを割り当てるためです。

または、インターフェイス(JAVA など)を定義し、何らかの方法で継承を使用する方法はありますか?

4

1 に答える 1