2016-06-29 47 views
6

Non riesco a ottenere il parametro del mio nome nella mia classe Employee! Non so perché ricevo un errore come this is not undefined! this è per l'oggetto corrente giusto! Non so come emettere il parametro del mio nome?questo non è definito errore nel costruttore di classi 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()); 

uscita effettiva

Uncaught ReferenceError: this is not defined 

Output previsto

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

L'errore effettivo ottengo in Firefox 48.0a2 è 'ReferenceError: | questa | usato non inizializzato nel costruttore della classe Employee'. – Xufox

+0

Ho visto la risposta a domande duplicate e testato! Ma ottieni un errore nella funzione '.map'. –

+0

Adesso va bene, sotto risposta ho risolto la mia domanda! Indico solo che la risposta a una domanda doppia non risolve il mio OP. –

risposta

8

È necessario chiamata super() costruttore prima di poter continuare a istanziare la classe:

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

    ... 
} 

JSBin