为什么在超类中定义时在子类中使用 super(qualifications) 会抛出 ReferenceError?
Why does using super(qualifications) in a subclass throw a ReferenceError when it was defined in the superclass?
class SchoolEmployee {
constructor(name, qualifications) {
this._name = name;
this._qualifications = qualifications;
this._holidaysLeft = 21;
}
get name() {
return this._name;
}
get qualifications() {
return this._qualifications;
}
get holidaysLeft() {
return this._holidaysLeft;
}
takeHolidays(days) {
this._holidaysLeft -= days;
}
}
class Teacher extends SchoolEmployee {
constructor(name, qualifications, subject) {
super(name);
super(qualifications); //THIS IS THE ERROR
this._subject = subject;
}
get name() {
return this._name;
}
get qualifications() {
return this._qualifications;
}
get subject() {
return this._subject;
}
}
let John = new Teacher('John', ['Maths', 'Physics'], 'Maths');
SchoolEmployee 是超级 class,我定义了 'qualifications' 是什么。据我所知,编写 super(qualifications) 会调用先前定义的 super class 的构造函数。目前我正在学习 Javascript,但我不明白哪里出了问题。有人可以帮助我吗?
如您所见,on mdn super() 调用了父级 class 的构造函数,并且您调用了它两次。你可能想要的是
class Teacher extends SchoolEmployee {
constructor(name, qualifications, subject) {
super(name, qualifications);
this._subject = subject;
}
}
SchoolEmployee 构造函数需要 2 个参数,您仅使用一个参数调用 super。尝试:
super(name, qualifications);
错误清楚地说 Super constructor may only be called once
。 Super 调用构造函数并且作为父构造函数接受两个 name, qualifications
将它们传入一个 super
.
class SchoolEmployee {
constructor (name, qualifications) {
this._name = name;
this._qualifications = qualifications;
this._holidaysLeft = 21;
}
get name() {
return this._name;
}
get qualifications() {
return this._qualifications;
}
get holidaysLeft() {
return this._holidaysLeft;
}
takeHolidays(days) {
this._holidaysLeft -= days;
}
}
class Teacher extends SchoolEmployee {
constructor (name, qualifications, subject) {
super(name, qualifications);
this._subject = subject;
}
get name() {
return this._name;
}
get qualifications() {
return this._qualifications;
}
get subject() {
return this._subject;
}
}
let John = new Teacher('John',['Maths', 'Physics'],'Maths');
class SchoolEmployee {
constructor(name, qualifications) {
this._name = name;
this._qualifications = qualifications;
this._holidaysLeft = 21;
}
get name() {
return this._name;
}
get qualifications() {
return this._qualifications;
}
get holidaysLeft() {
return this._holidaysLeft;
}
takeHolidays(days) {
this._holidaysLeft -= days;
}
}
class Teacher extends SchoolEmployee {
constructor(name, qualifications, subject) {
super(name);
super(qualifications); //THIS IS THE ERROR
this._subject = subject;
}
get name() {
return this._name;
}
get qualifications() {
return this._qualifications;
}
get subject() {
return this._subject;
}
}
let John = new Teacher('John', ['Maths', 'Physics'], 'Maths');
SchoolEmployee 是超级 class,我定义了 'qualifications' 是什么。据我所知,编写 super(qualifications) 会调用先前定义的 super class 的构造函数。目前我正在学习 Javascript,但我不明白哪里出了问题。有人可以帮助我吗?
如您所见,on mdn super() 调用了父级 class 的构造函数,并且您调用了它两次。你可能想要的是
class Teacher extends SchoolEmployee {
constructor(name, qualifications, subject) {
super(name, qualifications);
this._subject = subject;
}
}
SchoolEmployee 构造函数需要 2 个参数,您仅使用一个参数调用 super。尝试:
super(name, qualifications);
错误清楚地说 Super constructor may only be called once
。 Super 调用构造函数并且作为父构造函数接受两个 name, qualifications
将它们传入一个 super
.
class SchoolEmployee {
constructor (name, qualifications) {
this._name = name;
this._qualifications = qualifications;
this._holidaysLeft = 21;
}
get name() {
return this._name;
}
get qualifications() {
return this._qualifications;
}
get holidaysLeft() {
return this._holidaysLeft;
}
takeHolidays(days) {
this._holidaysLeft -= days;
}
}
class Teacher extends SchoolEmployee {
constructor (name, qualifications, subject) {
super(name, qualifications);
this._subject = subject;
}
get name() {
return this._name;
}
get qualifications() {
return this._qualifications;
}
get subject() {
return this._subject;
}
}
let John = new Teacher('John',['Maths', 'Physics'],'Maths');