2017-06-19 139 views
0

我爲我的工作做了一些任務,我完成了所有這些任務。但是我有一些問題,它不能像預期的那樣工作。當我嘗試添加一個新用戶的客戶類,例如:Javascript /擴展類/對象

var user3 = new Customer ("Sergiu", "Tataru"); 

當我訪問用戶3,我收到:

lastname: undefined 

爲什麼會這樣?

看到的結果,以明白我的意思

result

,我已經完成了任務:

  1. 使用一個Person類並擴展它的員工和客戶 類。
  2. Person對象具有專用名稱屬性和名稱的getter方法。
  3. 僱員類有兩個私人物業僱用日期和工資。它也有兩個屬性的getter方法。
  4. 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; 
 
}; 
 
}; 
 
}

+0

你爲什麼要調用'super(hireDate,salary);'?當它期待'(firstName,lastName)'... – evolutionxbox

+0

另外,'customer'構造函數沒有兩個參數。它有一個'contractNumber'。 ---我不認爲在這種情況下你想要的是經典的OOP,也許嘗試[duck-typing](https://stackoverflow.com/questions/3379529/duck-typing-in-javascript)? – evolutionxbox

+0

重新格式化和移動圖像內聯 – garyh

回答

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)

+0

是的,現在我看到了錯誤!THX –

0

您致電:

var user3 = new Customer ("Sergiu", "Tataru"); 

,但客戶的構造函數的參數是contractNumber,所以這是很正常的現象是沒有定義的姓氏。

該構造函數使用contractNumber調用Person的構造函數,因此您應該在名字中找到此值('Sergiu'),但在姓氏中沒有任何值傳遞。

編輯

你應該做這樣的事情:

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電話。

+0

但它不繼承父Person類的名字和姓氏的說法? –

+0

確實如此,但如果你初始化'(「的Sergiu」,「Tataru」)'你的對象,將有姓氏沒有價值。你應該得到'firstname =='Sergiu''和'contractNumber =='Sergiu''。繼承運行良好,不管用什麼方式管理構造函數和「超級」調用。 – sjahan

+0

好了,現在我明白了,我會作出一些改變,它應該工作,THX –