0

I have done some tasks for my job, I have done all of them. But I have some problems, it doesn't work as it supposed to. When I'm trying to add a new user for Customer class, for example:

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

And when I access user3, I receive:

lastname: undefined

Why so?

See the result to understand what I mean

result

The tasks that I have done:

  1. Use a Person class and extend it for the Employee and Customer classes.
  2. The Person object has a private name property and a getter method for the name.
  3. The Employee class has two private properties hire date and salary. It also has getter methods for the two properties.
  4. The Customer class has a private contract number property and a getter for the contract number.

The code:

//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;
};
};
}
garyh
  • 2,782
  • 1
  • 26
  • 28
  • Why are you calling `super(hireDate, salary);`? When it's expecting `(firstName, lastName)`... – evolutionxbox Jun 19 '17 at 09:22
  • Also, the `customer` constructor doesn't have two arguments. It has one `contractNumber`. --- I don't think classical OOP is what you want in this case, maybe try [duck-typing](https://stackoverflow.com/questions/3379529/duck-typing-in-javascript)? – evolutionxbox Jun 19 '17 at 09:23
  • reformatted and moved image inline – garyh Jun 20 '17 at 09:53

2 Answers2

0

I think there are some issues with you super calls.

//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

You call:

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

but the Customer's constructor's arguments are contractNumber, so it's quite normal the lastname is not defined.

This constructor calls the Person's constructor with the contractNumber, so you should find this value ('Sergiu' here), in the firstname, but there is nothing passed in the lastname.

EDIT

You should do something like that:

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;
};
};
}

Look at the constructor interface and at the super call.

sjahan
  • 5,720
  • 3
  • 19
  • 42
  • But it doesn't inherit the firstname and lastname argument from the parent Person class? – Misha Lopez Jun 19 '17 at 09:27
  • It does, but if you initialize your object with `('Sergiu', 'Tataru')`, there will be no value for lastname. You should get `firstname == 'Sergiu'` and `contractNumber == 'Sergiu'`. Inheritance is working well, what's not working is the way you manage the constructors and the `super` calls. – sjahan Jun 19 '17 at 09:29
  • Ok, now I understand, i will make some changes and it should work, thx – Misha Lopez Jun 19 '17 at 09:36
  • -> thank you for your extended answer !! it's all clear now, it works as it should ! – Misha Lopez Jun 19 '17 at 09:39