2017-06-19 193 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 –