'Why do i keep getting undefined when accessing a inherited property? [duplicate]

I'm trying to use prototypal inheritance and its not working, everytime i try i keep getting undefined:

function HospitalEmployee(name) {
  this._name = name;
  this._remaningVacationDays = 20;
}

function Nurse() {}

Nurse.prototype = Object.create(HospitalEmployee.prototype)

const nurseOlynyk = new Nurse("Olynyk", ["Trauma", "Genetics"])

console.log(nurseOlynyk._remainingVacationDays) //Prints undefined

What am i doing wrong?



Solution 1:[1]

I need use Class and Extends for it same as :

class HospitalEmployee {
  constructor(name){
    this._name = name;
    this._remaningVacationDays = 20;
  }
}
class Nurse extends HospitalEmployee {
  constructor(name, otherParams){
    super(name);
    this.otherParams = otherParams;
  }
}
const nurseOlynyk = new Nurse("Olynyk", ["Trauma", "Genetics"])
console.log(nurseOlynyk)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xupitan