如何解决松散继承的目的
我在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);
如预期的那样,他们有自己的道具和方法。
-
我使用了发现的_inheritsLoose方法
Human.prototype = Object.create(Superhuman.prototype); Human.prototype.constructor = Superhuman; Human.__proto__ = Superhuman;
,并希望以下行必须为真
// Expecting its true now???
console.log(Human.prototype === Superhuman.prototype);
和
// Expecting its available in Human now???
Superhuman.prototype.fly = () => console.log("I fly....");
但是没有用。所以我的问题是_inheritsLoose方法的目的是什么,我在做错什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。