Loosely Inheritance purpose

Viewed 94

I found the following interesting method in the babel compiler in and im totally new to js:

  export default function _inheritsLoose(subClass, superClass)  {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    subClass.__proto__ = superClass;   } ;

So I assumed, its to generate loosely coupled inheritance between two objects and I tried the following:

  1. I created two simple objects

    class Human{
        constructor(name, surname){
         this.Name = name;
         this.Surname = surname;
        }
    
        rennen(){
            console.log("Ich renne...");
        }
    }
    
    class Superhuman{
        constructor(age, weight) {
            this.Age = age;
            this.Weight = weight;
        }
    
        ausruhen(){
            console.log("Ich ruhe aus...");
        }
    }
    
  2. Created them and logged prototype

    var a = new Human("Max", "Mustermann");
    var b = new Superhuman(4, 80);
    
    console.log("Human");
    console.log(Human.prototype);
    
    console.log("Superhuman");
    console.log(Superhuman.prototype);
    

As expected they have their own props and methods.

  1. I used the _inheritsLoose method that i found

    Human.prototype = Object.create(Superhuman.prototype);
    Human.prototype.constructor  = Superhuman;
    Human.__proto__ = Superhuman;
    

and expected that the following line has to be true

   // Expecting its true now???
   console.log(Human.prototype === Superhuman.prototype);

and

   // Expecting its available in Human now???
   Superhuman.prototype.fly = () => console.log("I fly....");

But it did not work. So my question is what is the purpose of the _inheritsLoose method and what am I doing wrong?

0 Answers
Related