Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement partial inheritance in javascript?

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?

like image 798
Ankur Gupta Avatar asked May 29 '26 13:05

Ankur Gupta


1 Answers

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 () {}
like image 97
neaumusic Avatar answered Jun 01 '26 03:06

neaumusic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!