我为我的工作完成了一些任务,我已经做了所有这些工作。但我有一些问题,它没有按预期工作。当我尝试为Customer类添加新用户时,例如:
var user3 = new Customer ("Sergiu", "Tataru");
当我访问user3时,我会收到:
lastname: undefined
为什么会这样?
查看结果以了解我的意思
我完成的任务:
代码:
//4)Create a Person class
class Person{
constructor(firstName, lastName) {
this.firstname = firstName;
this.lastname = lastName;
var _name = name;// create a private name property for the Person class
// create a getter method for the name for the Person class
this.getName = function () {
return _name;
};
this.getFullName = function() {
return this.firstname+ " " + this.lastname;
};
}
}
// extend Person class for the Employee and Customer classes.
class Employee extends Person {
constructor(hireDate, salary){
super(hireDate, salary);
var _hiredate = hireDate; // create a private property hire date for Employee class
var _salary = salary; // create a private property salary for Employee class
// create a getter method for the hire date s
this.getHireDate = function(){
return _hiredate;
};
// create a getter method for the salary
this.getSalary = function(){ //varianta alternativa: Employee.prototype.getSalary = function(){
return _salary;
};
}
}
class Customer extends Person {
constructor(contractNumber){
super(contractNumber);
var _contractNumber = contractNumber; // create a private contract number for Customer class
//create a getter for the contract number.
this.getcontractNumber = function(){
return _contractNumber;
};
};
}
答案 0 :(得分:0)
我认为您super
来电时存在一些问题。
//4)Create a Person class
class Person{
constructor(firstName, lastName) {
this.firstname = firstName;
this.lastname = lastName;
var _name = name;// create a private name property for the Person class
// create a getter method for the name for the Person class
this.getName = function () {
return _name;
};
this.getFullName = function() {
return this.firstname+ " " + this.lastname;
};
}
}
// extend Person class for the Employee and Customer classes.
class Employee extends Person {
constructor(firstname, lastname, hireDate, salary){
super(firstname, lastname);
var _hiredate = hireDate; // create a private property hire date for Employee class
var _salary = salary; // create a private property salary for Employee class
// create a getter method for the hire date s
this.getHireDate = function(){
return _hiredate;
};
// create a getter method for the salary
this.getSalary = function(){ //varianta alternativa: Employee.prototype.getSalary = function(){
return _salary;
};
}
}
class Customer extends Person {
constructor(firstname, lastname, contractNumber){
super(firstname, lastname);
var _contractNumber = contractNumber; // create a private contract number for Customer class
//create a getter for the contract number.
this.getcontractNumber = function(){
return _contractNumber;
};
};
}
var user3 = new Customer ("Sergiu", "Tataru", 999);
console.log(user3.lastname)
答案 1 :(得分:0)
你打电话:
var user3 = new Customer ("Sergiu", "Tataru");
但是客户的构造函数的参数是contractNumber
,所以没有定义姓氏,这很正常。
这个构造函数使用contractNumber调用Person的构造函数,所以你应该在firstname中找到这个值('Sergiu'
),但是在lastname中没有传递。
修改强>
你应该做那样的事情:
class Customer extends Person {
constructor(firstname, lastname, contractNumber){
super(firstname, lastname);
var _contractNumber = contractNumber; // create a private contract number for Customer class
//create a getter for the contract number.
this.getcontractNumber = function(){
return _contractNumber;
};
};
}
查看构造函数界面和super
调用。