3

I have gone in an interview yesterday where interviewer asked me about partial inheritance, about which I am clueless. I have mentioned 2 code blocks below as per my understanding. Let me know whatever I am doing is correct or not for partial inheritance.

Code Block 1: Can we call this partial inheritance?

var Person = function(){}

Person.prototype.getName = function(){
    return this.name;
}

Person.prototype.setName = function(name){
    this.name = name;
}

var Author = function(name, books){
    this.books = books;
    Person.prototype.setName.call(this, name); <<---- Is this a way of doing partial inheritance?
}

Author.prototype.getBooks = function(){
    return this.books;
}

var author = new Author('Auth1', ['b1','b2']);

Code Block 2: How to inherit only "setName" function from "Person" constructor? Is it possible?

var Person = function(){
    this.setName = function(name){
        this.name = name;
    };
}

Person.prototype.getName = function(){
    return this.name;
}

var Author = function(name, books){
    this.books = books;
}

Author.prototype.getBooks = function(){
    return this.books;
}

var author = new Author('Auth1', ['b1','b2']);

Do explain the authenticity of above 2 code blocks?

I even don't know whether examples quoted above are right or not. If they are not correct than please throw some light on partial inheritance in JavaScript.

Question: I just want to inherit only setName method from Person constructor to Author constructor. Is that possible?

5
  • You can find very clean explanation here : w3schools.com/js/js_object_prototypes.asp Commented Dec 7, 2017 at 7:09
  • It doesn't talk about partial inheritance. Does it exist or not? If not, than in what context can an interviewer ask this question? Commented Dec 7, 2017 at 7:22
  • When you say "inherit" the function, what you really mean is get a function which utilizes this (unbound). If you bind something, this is replaced with the actual instance object, and is no longer a generic class method that could be called in any context. The difference between call and apply is that apply works with arguments and arrays (notice they all start with 'a') Commented Dec 7, 2017 at 8:38
  • I have never heard the term "partial inheritance" before. Commented Dec 8, 2017 at 0:02
  • Why is setName created inside the Person constructor and not put as a normal method on Person.prototype? It doesn't even use anything from the constructor scope. Commented Dec 8, 2017 at 0:04

1 Answer 1

1

From reading your description, I don't think you're familiar with context and the concept of this, which is a wildcard that can represent any object. Also worth noting is that the prototype chain is just a specific place to look if a property appears to be undefined on a class instance. An instance is just a regular object that's returned after running the constructor (via 'new' keyword), where the function class.prototype.asdf is the same function as instance.asdf if instance.asdf isn't defined

const OtherClass = function () {};
const MyClass = function () {};
      MyClass.prototype.logThis = function () { console.log(this); };

const otherInstance = new OtherClass();
const myInstance = new MyClass();

myInstance.logThis(); // logs myInstance
myInstance.logThis.call(otherInstance, 'param1', 'param2'); // logs otherInstance
myInstance.logThis.apply(otherInstance, ['param1', 'param2']); // logs otherInstance

The simple way to remember what 'this' is -- whatever's 'to the left of the dot' at execution or invocation time, not the function scope where it's written. This is why setTimeout and other async things like Promises won't use the 'this' you might expect, unless you bound it already.

You can write a generic, shared function that operates with unknown, and include it in different objects.

Person.prototype.setName = Author.prototype.setName = function setName () {}
Sign up to request clarification or add additional context in comments.

8 Comments

I just want to inherit only setName method from Person constructor to Author constructor. Is that possible?
@AnkurGupta constructor is a special method on the class, I think you mean onto the prototype chain, so that all Author instances refer to the same function. You can just write Author.prototype.setName = Person.prototype.setName and you're good to go
But why should I repeat the same method when I have it in some other constructor. It will introduce code redundancy. Also I don't want to create any other constructor. Question is how to implement partial inheritance with above example (only having 2 constructors).
if you're using class inheritance, you shouldn't define a shared method in each instance via the constructor, you should have Person.prototype.setName = function () {}
Person.prototype.setName = Author.prototype.setName = function setName () {}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.