Javascript 松散继承目的

Javascript 松散继承目的,javascript,inheritance,Javascript,Inheritance,我在中的babel编译器中发现了以下有趣的方法,并且我对js是全新的: export default function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass;

我在中的babel编译器中发现了以下有趣的方法,并且我对js是全新的:

  export default function _inheritsLoose(subClass, superClass)  {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    subClass.__proto__ = superClass;   } ;
因此,我假设在两个对象之间生成松散耦合的继承,并尝试了以下方法:

  • 我创建了两个简单的对象

    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...");
        }
    }
    
  • 创建它们并记录原型

    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);
    
  • 正如所料,他们有自己的道具和方法

  • 我使用了我找到的(u inheritsLoose)方法

    Human.prototype = Object.create(Superhuman.prototype);
    Human.prototype.constructor  = Superhuman;
    Human.__proto__ = Superhuman;
    
  • 并期望下面这句话一定是真的

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


    但它没有起作用。所以我的问题是_inheritsLose方法的目的是什么?我做错了什么?

    你不能更改类的
    prototype
    属性。巴贝尔为什么这样做?你怎么知道参数
    子类和
    超类实际上是类?命名并调用prototype。
    
       // Expecting its available in Human now???
       Superhuman.prototype.fly = () => console.log("I fly....");