Javascript Classes Inheritance V


#1

I am getting this error
why??

/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:32
this._certifications.push(newCertification);
^

TypeError: Cannot read property ‘push’ of undefined
at Object. (/home/ccuser/workspace/learn-javascript-classes-classes-inheritance-v/main.js:32:21)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

the code is …

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._certifications = certifications;
  } 
  
  get certifications() {
    return this._certifications
  }
  
}

this._certifications.push(newCertification);

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


#2

If you read the code at that line, you’ll see that you’re trying to access the property named push of this._certifications

With that established you can now consider whether this._certifications should have that property, and if so, when it should have been defined - clearly that didn’t happen so then you know what went wrong.
Or, if it shouldn’t have that property, then you probably shouldn’t trying to be accessing it either, perhaps you meant to get some other value at some other location in your program’s internal state.

Also, if you look at your code, you’ll find that you’re referring to this outside of a class. Does that make sense?