gpt4 book ai didi

javascript - 这不是 javascript 类构造函数中定义的错误?

转载 作者:数据小太阳 更新时间:2023-10-29 03:49:08 26 4
gpt4 key购买 nike

<分区>

我无法在我的 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());

实际输出

Uncaught ReferenceError: this is not defined

预期输出

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com