我为我的工作做了一些任务,我完成了所有这些任务。但是我有一些问题,它不能像预期的那样工作。当我尝试添加一个新用户的客户类,例如:Javascript /扩展类/对象
var user3 = new Customer ("Sergiu", "Tataru");
当我访问用户3,我收到:
lastname: undefined
为什么会这样?
看到的结果,以明白我的意思
,我已经完成了任务:
- 使用一个Person类并扩展它的员工和客户 类。
- Person对象具有专用名称属性和名称的getter方法。
- 雇员类有两个私人物业雇用日期和工资。它也有两个属性的getter方法。
- Customer类别具有私人合约号码属性和合约号码的获取者。
代码:
//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;
};
};
}
你为什么要调用'super(hireDate,salary);'?当它期待'(firstName,lastName)'... – evolutionxbox
另外,'customer'构造函数没有两个参数。它有一个'contractNumber'。 ---我不认为在这种情况下你想要的是经典的OOP,也许尝试[duck-typing](https://stackoverflow.com/questions/3379529/duck-typing-in-javascript)? – evolutionxbox
重新格式化和移动图像内联 – garyh