I'm learning JavaScript prototyping. Here Employee is parent, I inherited from it in my Programmer prototype, but when I try to run the favoriteLanguage method of my child prototype(i.e Programmer), it's showing that favoriteLanguage is not a function. I tried reading from Mozilla documentation, but I couldn't understand if the stuff was relevant or not! Can anybody help me in simple terms please!
Edit: I know I can use class, but I want to learn why it's not working in this case!
Here's the code:
function Employee(givenName, givenExperience, givenDivision){
this.name = givenName;
this.experience = givenExperience;
this.division = givenDivision;
}
Employee.prototype.slogan=function(){
return `I am ${this.name} and this company is the best`;
}
Employee.prototype.joiningYear=function(){
return 2020 - this.experience;
}
function Programmer(givenName, givenExperience, givenDivision, language, github){
Employee.call(this,givenName, givenExperience, givenDivision);
this.language = language;
this.github = github;
}
Programmer.prototype.favoriteLanguage = function(){ //Error part
if (this.language == 'python'){
return 'Python';
}
else{
return 'JavaScript';
}
}
Programmer.prototype = Object.create(Employee.prototype);
Programmer.prototype.constructor = Programmer;
//Object.setPrototypeOf(Programmer.prototype, Employee.prototype);
let arju = new Programmer("Arju Aman",0,"Developer","javaScript","arjuaman");
console.log(arju);
// console.log(arju.joiningYear());
// console.log(arju.slogan());
console.log(arju.favoriteLanguage()); //called here
Programmer.prototype = Object.create(Employee.prototype);overrides everything which was declared under `Programmer.prototypeso far. Try to switch the order!new Employeeto create theProgrammer.prototypeobject, or don't callEmployeefromProgrammercorrectly, etc. You didn't. Nice one!