【问题标题】:Javascript/ Extended classes/ ObjectsJavascript/扩展类/对象
【发布时间】:2017-11-21 10:30:26
【问题描述】:

我已经为我的工作完成了一些任务,我已经完成了所有这些任务。但是我有一些问题,它不能按预期工作。当我尝试为 Customer 类添加新用户时,例如:

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

当我访问 user3 时,我收到:

lastname: undefined

为什么会这样?

查看结果以了解我的意思

我已经完成的任务:

  1. 使用 Person 类并为 Employee 和 Customer 扩展它 类。
  2. Person 对象具有私有名称属性和名称的 getter 方法。
  3. Employee 类有两个私有属性租用日期和薪水。它还具有用于这两个属性的 getter 方法。
  4. 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


【解决方案1】:

我认为您的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)

【讨论】:

    【解决方案2】:

    你打电话:

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

    但是客户的构造函数的参数是contractNumber,所以没有定义姓氏是很正常的。

    这个构造函数使用contractNumber调用Person的构造函数,所以你应该在firstname中找到这个值(这里是'Sergiu'),但lastname中没有传递任何内容。

    编辑

    你应该这样做:

    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 调用。

    【讨论】:

    • 但是它没有继承父类的名字和姓氏参数?
    • 确实如此,但是如果你用('Sergiu', 'Tataru') 初始化你的对象,lastname 将没有任何值。你应该得到firstname == 'Sergiu'contractNumber == 'Sergiu'。继承运行良好,但管理构造函数和 super 调用的方式不起作用。
    • 好的,现在我明白了,我会做一些改变,它应该可以工作,谢谢
    • -> 感谢您的扩展回答!现在一切都清楚了,它可以正常工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多