我爲我的工作做了一些任務,我完成了所有這些任務。但是我有一些問題,它不能像預期的那樣工作。當我嘗試添加一個新用戶的客戶類,例如: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