JavaScript Object/ Class/ Getter methods

239
16 июня 2017, 11:07

Добрый день) Мне нужно выполнить эти задания что бы пройти собеседование в одной компаний, на первую работу) Вот задания:

  1. Используйте класс Person и расширьте его для классов Employee и Customer;

  2. Объект Person имеет приватное свойство "name" и getter метод для "name".

  3. Класс Employee имеет две приватные свойства: "hire date"(дата принятия на работу) и "salary"(зарплата). Они также имеют методы getter для этих своиств.

  4. Класс Customer имеет приватное свойство "contract number" (номер договора) и getter для номера договора.

//4)Create a Person class 
class Person{ 
  constructor(firstName, lastName) { 
  this.firstname = firstName; 
  this.lastname = lastName; 
  this._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 this._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){ 
  this._hiredate = hireDate; // create a private property hire date for  Employee class 
  this._salary = salary; // create a private property salary for  Employee class 
 
  // create a getter method for the hire date 
  this.getHireDate = function(){ 
  return this._hiredate; 
}; 
  // create a getter method for the salary 
  this.getSalary = function(){  
  return this._salary; 
}; 
} 
} 
 
 
class Customer extends Person { 
constructor(){ 
 
this._contractNumber = contractNumber; // create a private contract number for Customer class 
 
 
//create a  getter for the contract number. 
this.getcontractNumber = function(){ 
return this._contractNumber; 
}; 
}; 
}

Вроде сделал как знаю на данный момент, но выдаёт ошибки. Я закрепил фото. И может у кого-то есть советы, и скажет как сделать правильно. Буду очень благодарен всем!!!

Answer 1

Bроде сейчас попробовал добавил "super()", и уже не выдаёт ошибку, и можно создавать новый объекты с классом Customer и Employee ) вроде работает, надеюсь правильно сделал.

class Person{ 
  constructor(firstName, lastName) { 
  this.firstname = firstName; 
  this.lastname = lastName; 
  this._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 this._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); 
  this._hiredate = hireDate; // create a private property hire date for  Employee class 
  this._salary = salary; // create a private property salary for  Employee class 
 
  // create a getter method for the hire date s 
  this.getHireDate = function(){ 
  return this._hiredate; 
}; 
  // create a getter method for the salary 
  this.getSalary = function(){  //varianta alternativa:  Employee.prototype.getSalary = function(){ 
  return this._salary; 
}; 
} 
} 
 
 
class Customer extends Person { 
constructor(contractNumber){ 
 
super(contractNumber); 
this._contractNumber = contractNumber; // create a private contract number for Customer class 
 
 
//create a  getter for the contract number. 
this.getcontractNumber = function(){ 
return this._contractNumber; 
}; 
}; 
}

READ ALSO
Получить файл удаленно по маске!

Получить файл удаленно по маске!

Люди добрые, хочу просто получить удаленно файл по маске с сервера и сохранить к себе в папкуВсе, что перед

195
Как скрыть кнопку от всех, кроме пользователя у которого

Как скрыть кнопку от всех, кроме пользователя у которого

Как скрыть кнопку от всех, кроме пользователя, который находится в таблице users и имеет e-mail: igor@gmailcom Возможно такое реализовать? К примеру, такая...

248
Печать квадратов целых чисел в цикле [требует правки]

Печать квадратов целых чисел в цикле [требует правки]

Почему получается 1491625 ? Не могу понять смысл цикла, идет 1, потом 1++ = 2, далее 2 * 2 = 4, 4++ = 5, 5 * 5 = 25

256