Is this an undefined error in the javascript class constructor?

I cannot get my name parameter in the Employee class! I do not know why I am getting an error, for example this is not undefined ! this for the current object is correct! I can not specify my name parameter?

 class Person { constructor(n, a) { var p = this; pn = n; pa = a; p.total = 0; pamap(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()); 

Actual output

 Uncaught ReferenceError: this is not defined 

Expected Result

 My name is Min Min David and My yearly income is 1333 
+6
source share
1 answer

You need to call the super() constructor before you can continue to instantiate the class:

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

Jsbin

+8
source

All Articles