私は自分の仕事のためにいくつかのタスクを実行しました。しかし、私はいくつかの問題を抱えています。Javascript/Extended classes/Objects
var user3 = new Customer ("Sergiu", "Tataru");
そして、私はUSER3にアクセスするときに、私が受け取る::私には、例えば、Customerクラスの新しいユーザーを追加しようとしているときに
なぜlastname: undefined
?
私は私が行っている
タスク意味を理解するために結果を参照してください:
- をPersonクラスを使用し、従業員や顧客 クラスのためにそれを拡張します。
- Personオブジェクトには、private nameプロパティとその名前の取得メソッドがあります。
- Employeeクラスには、2つのプライベートプロパティがあり、日付と給与が雇用されています。また、2つのプロパティのゲッターメソッドもあります。
- Customerクラスには、プライベート契約番号プロパティと契約番号ゲッターがあります。
コード:私はあなた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(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'コンストラクタには2つの引数がありません。 1つの 'contractNumber'を持っています。 ---私は古典的なOOPがあなたがこの場合にしたいと思うものではないと思うかもしれません。おそらく[ダックタイピング](https://stackoverflow.com/questions/3379529/duck-typing-in-javascript)を試してみてください。 – evolutionxbox
イメージを再フォーマットして移動しました – garyh