我无法在Employee类中获取我的名字参数!我不知道为什么我会得到错误this is not undefined!this是对当前对象权利!我不知道如何输出我的名字参数?
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());
Run Code Online (Sandbox Code Playgroud)
实际产出
Uncaught ReferenceError: this is not defined
Run Code Online (Sandbox Code Playgroud)
预期产出
My name is Min Min David and My yearly income is 1333
Run Code Online (Sandbox Code Playgroud)
Tim*_*imo 13
在继续实例化类之前,需要先调用super()构造函数:
class Employee extends Person {
constructor(name, age) {
super(name, age);
this.name = name;
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3743 次 |
| 最近记录: |