2017-06-01 4 views
0

簡単な質問です。同じ名前の2つのメソッドを持つ2つのクラスがある場合... "applyMixins"を呼び出すと、これらの2つのクラスを(新しいクラスに)実装したい場合はどうなりますか?実装時に同じ名前のメソッドはどうなりますか?

ありがとうございました。

+0

あなた自身で試しましたか?何が起こった?コードを共有する –

+1

TypeScriptコンパイラは(多少の)可読なJavaScriptを出力します。出力ファイルを開くと、これを見つけることができます。 –

答えて

0
class First{ 
     name: string; 
     methodA(name: string){ 
      this.name= name; 
     } 
    } 

    class Second{ 
     id: string; 
     methodA(id: string){ 
      this.id= id; 
     } 
    } 

    class newClass implements First, Second { 
     id: string; 
     name: string; 
     methodA:()=> void; 
    } 
    applyMixins(newClass, [First, Second]); 
    function applyMixins(derivedCtor: any, baseCtors: any[]) { 
     baseCtors.forEach(baseCtor => { 
      Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => { 
       derivedCtor.prototype[name] = baseCtor.prototype[name]; 
      }); 
     }); 
    } 
    var Pluto = new newClass(); 

    console.log(Pluto.methodA); 

これは私のコードです(何が起こるかは一例です)。私のconsolログの結果は、SECOND CLASSのメソッドのように書かれた関数です。あれは正しいですか?ありがとう

関連する問題