【发布时间】:2017-11-21 10:30:26
【问题描述】:
我已经为我的工作完成了一些任务,我已经完成了所有这些任务。但是我有一些问题,它不能按预期工作。当我尝试为 Customer 类添加新用户时,例如:
var user3 = new Customer ("Sergiu", "Tataru");
当我访问 user3 时,我收到:
lastname: undefined
为什么会这样?
查看结果以了解我的意思
我已经完成的任务:
- 使用 Person 类并为 Employee 和 Customer 扩展它 类。
- Person 对象具有私有名称属性和名称的 getter 方法。
- Employee 类有两个私有属性租用日期和薪水。它还具有用于这两个属性的 getter 方法。
- Customer 类具有私有合同编号属性和合同编号的 getter。
代码:
//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)... -
另外,
customer构造函数没有两个参数。它有一个contractNumber。 --- 在这种情况下,我不认为经典的 OOP 是你想要的,也许试试 duck-typing? -
重新格式化并移动图像内联
标签: javascript class oop object getter