Inheritance V


#1
class HospitalEmployee {
  constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }
  
  get name() {
    return this._name;
  }
  
  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
  }
}

class Nurse extends HospitalEmployee {
  constructor(name, certifications) {
    super(name);
    this._remainingVacationDays = 20;
    this._certifications = certifications;
  } 
  
  get certifications() {
    return this._certifications;
  }
  
  addCertification(newCertification) {
    this._certifications = this._certifications.push(newCertification);
  }
}

const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
nurseOlynyk.takeVacationDays(5);
console.log(nurseOlynyk.remainingVacationDays);

nurseOlynyk.addCertification('Genetics');
console.log(nurseOlynyk.certifications);

// Why is the output of the last line (console.log(nurseOlynyk.certifications):wink: a number and not the array with the strings?

Thank you very much!


#2

because of what you do here:

this._certifications = this._certifications.push(newCertification);

Any ideas what you do wrong there? Take a minute to think about it, let me know if you need more help


#3

Ah ok, yeah I assign a new value and overwrite the old one instead of adding another string to my array. Still, I would think that the output of console.log(nurseOlynyk.certifications) would then be [‘Genetics’] and not a number?

Thanks!


#4

read the docs:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

it explains how the method works. So the person who wrote the push method to decide to return the new length of the array (after adding the element)

by doing variable_name = method_call() you store the returned result in the variable_name


#5

Aha sure, now I understand. I overwrote my variable with the length of the array^^
Thanks again.


#6

the problem with returning an array would be that you then have a duplicate array, which is very memory inefficient. And the length seems a valuable thing to return, better then undefined