2016-06-29 9 views
6

私の名前パラメータをEmployeeクラスで取得できません!なぜ私はエラーのようになるのかわからないthis is not undefinedthisは現在のオブジェクト用です!私の名前パラメータを出力する方法はありませんか?これはjavascriptクラスのコンストラクタで定義されていないエラーですか?

class Person { 
    constructor(n, a) { 
     var p = this; 
     p.n = n; 
     p.a = a; 
     p.total = 0; 
     p.a.map(x => p.total += parseInt(x)); //get total salary  
    } 
    firstName() { 
     return this.n = "Min Min "; 
    } 
    displayMsg() { 
     return " and My yearly income is " + this.total; 
    } 
} 

class Employee extends Person { 
    constructor(name, age) { 
     this.name = name; 
    } 
    lastName() { 
     return this.name; 
    } 
    Show() { 
     return "My name is " + super.firstName() + this.lastName() + super.displayMsg(); 
    } 
} 
emp = new Employee("David", [123, 456, 754]); 
console.log(emp.Show()); 

実際の出力

Uncaught ReferenceError: this is not defined 

予想される出力

My name is Min Min David and My yearly income is 1333 
+0

私はFirefoxの48.0a2に入る実際のエラーは 'にReferenceError次のとおりです。|この| Employeeクラスのコンストラクタでは初期化されていません。 – Xufox

+0

私は重複した質問の答えを見て、テストしました!しかし、 '.map'関数でエラーが発生します。 –

+0

それは今、私の質問を解決した答えの下で大丈夫です!私は重複した質問の答えは私のOPを解決しないことを指摘します。 –

答えて

8

あなたは呼び出し、あなたのクラスをインスタンス化を続行する前に、まずsuper()コンストラクタする必要があります。

class Employee extends Person { 
    constructor(name, age) { 
     super(name, age); 
     this.name = name; 
    } 

    ... 
} 

JSBin

関連する問題