Javascript 松散继承目的
我在中的babel编译器中发现了以下有趣的方法,并且我对js是全新的: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;
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);
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....");