有人可以解释一下为什么如果我调用greet函数,firstName和lastName是未定义的吗? 似乎两者都设置正确,所以我真的不知道出了什么问题。我应该做什么来解决它。
'use strict';
class Person {
constructor() {
this.firstName = 'James';
this.lastName = 'Bond';
}
greet() {
console.log(`Hello ${this.firstname} ${this.lastname}`);
}
}
class Employee extends Person {
constructor() {
super();
this.salary = 1234;
}
}
var employee = new Employee();
console.log(employee.firstName); //James
console.log(employee.lastName); //Bond
console.log(employee.salary); //1324
employee.greet(); //Hello undefined undefined
最佳答案
JavaScript 非常区分大小写... firstname != firstName
你想要:
console.log(`Hello ${this.firstName} ${this.lastName}`);
^ ^
关于JavaScript 继承 - 值未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34887319/