2

I have given many interviews on javascript. One question that is always common is "How Inheritance is achieved, explain with an example".

What I explain to the interviewer is more or less same as given in MDN site

My piece of code:

//Parent Class
var Employee = function(name,salary) {
    this.name = name;
    this.salary = salary;
}
Employee.prototype.WelcomeMessage = function() {
    return ("Welcome "+this.name)
}
Employee.prototype.getSalary = function() {
    return this.salary
}

//Child Class
var Infra = function(name,salary) {
    Employee.call(this,name,salary);
    //Child should have its own get Salary method
    Infra.getSalary = function() {
        return "this is salary for child"
    }
}
Infra.prototype = Object.create(Employee.prototype);

Infra.prototype.constructor = Infra ;

// Created instance of child to achieve Inheritance

var childInstance = new Infra("Jack","$1000")
childInstance.name //Return Jack
childInstance.WelcomeMessage() //Return Welcome Jack
childInstance.getSalary //return "this is salary for child" - this method was 
                        //  overridden by child

But every time i gave this example interviewer is not ok with this solution . They say in this inheritance example "Infra.prototype.constructor = Infra ;" is used , which is not recommended . You should use Object.assign with self invoking functions to achieve inheritance .

I generally defend it by saying this also achieve desired result and the same is given in MDN.

What is the correct way to achieve inheritance?

James Z
  • 12,209
  • 10
  • 24
  • 44
Aditya
  • 431
  • 1
  • 7
  • 21
  • 1
    Well maybe if company asks about such questions and its that important for them, its good indication you should not join them. Its 2018, classes are out for pretty long, if you want to write modern code, you do not need superior knowledge of prototypes and all its specifics. They should be more focused about your analytical thinking, about BE/FE experience, about some standard behaviour of code that is used on daily bases, let you program something that shows your ability with think and develop... – libik Jul 17 '18 at 10:26
  • 1
    `class ... extends`. End of story. – trincot Jul 17 '18 at 18:41
  • This is more job-hunting advice than code advice, maybe, but: when disagreeing with your interviewer, instead of defending your choice with "well, it works, and I saw it done this way on MDN once", try explaining *why* you prefer your method, or at least demonstrate that you understand the difference between it and what they prefer. (See also https://stackoverflow.com/questions/8453887/why-is-it-necessary-to-set-the-prototype-constructor particularly TJ Crowder's answer) – Daniel Beck Jul 17 '18 at 19:00
  • `Object.assign(prototype, {constructor: …})` over `prototype.constructor = …`? Well why not, though with a single property there's no advantage. But "self invoking functions"? That has nothing to do with inheritance. – Bergi Jul 17 '18 at 19:49

0 Answers0