JavaScript多继承的高效解决方案

标签 javascript oop inheritance mixins composition

为什么这个问题不是重复的

这个答案javascript inheritance from multiple objects 没有解决我的问题(尽管它已被标记为与我之前的问题重复),因为它不可扩展,因为它违反了 DRY 原则。

为此,必须像这样手动引用每个方法:

Foo2.prototype.a = function() { /*code...*/};
Foo2.prototype.b = function() { /*code...*/};
Foo2.prototype.c = function() { /*code...*/};
Foo2.prototype.d = function() { /*code...*/};
//and so on and so on...

如果我有几十个包含许多方法的类怎么办?我真的应该在我的源代码中一遍又一遍地手动复制粘贴每个类的相同引用吗?虽然此解决方案适用于数量非常少的类,但它不适用于使用数十个或数百个类的大型应用程序。

我要解决的问题

enter image description here

我正在尝试实例化对象,这些对象必须使用 new 关键字继承 AnimalFlying_object 的所有属性和方法。

var objA = new Fish(),
    objB = new Bird(),
    objC = new UFO();

棘手的部分是 AnimalFlying_object 不能有父子关系。

我知道 JavaScript 没有实现多重继承的 native 方法,所以我发布这个问题是为了获得一些帮助,以找到针对此问题的自定义、可扩展的解决方案。

代码示例和预期行为

var Living_being = function() { this.className = 'Living_being'; };

var Animal = function() {
    this.className = 'Animal';
    this.vector = {x: 0, y: 0};
}
Animal.prototype = new Living_being();
Animal.prototype.getClassName = function() { console.log('Instance of... '+ this.className); };
Animal.prototype.get_vector = function() { console.log(this.vector); }

var Flying_object = function() {
    this.className = 'Flying_object';
    this.value = 'some value';
}
Flying_object.prototype.getClassName = function() { console.log('Instance of... '+ this.className); };
Flying_object.prototype.get_val = function() { console.log(this.value); }

// So far so good...
var UFO = function() {};
UFO.protoype = new Flying_object(); //classical inheritance
var Fish = function() {};
Fish.protoype = new Animal(); //classical inheritance
// Now the tricky part: how to make Bird share all of the methods and properties of Animal and Flying_object ?
var Bird = function() {};
Bird.prototype = new ....(); //pseudocode where .... is a class containing all the properties of Animal and Flying_object

var instance = new Bird();

//expected result:
instance.getClassName();//--> Instance of...
instance.get_vector();  //--> {x: 0, y: 0}
instance.get_val();     //--> 'some value'

这就是我卡住的地方。如何让 Bird 从 AnimalFlying_object 继承? 任何见解将不胜感激。

最佳答案

这是我在某个时候提出并放弃的可行解决方案,因为我认为可能有更好的解决方案。

@Mörre:我不确定这是你在评论中建议我做的:这就是你所说的对象组合吗?还是我在这里全错了?

演示:https://jsfiddle.net/Lau1989/4ba8yrc8/1/

function mix(classA, classB) {
    var instanceA = new classA(),
        instanceB = new classB();
    for (var prop in instanceA) {
        instanceB[prop] = instanceA[prop];
    }
    return instanceB;
}

var Bird = function() { this.className = 'Bird'; };
Bird.prototype = mix(Animal, Flying_object);
var instance = new Bird();

关于JavaScript多继承的高效解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47399264/

相关文章:

Javascript 公共(public)方法参数作为私有(private)变量

java - 在工厂中使用反射

java - java中如何继承@JsonFormat注解?

javascript - OOP 继承和对象实例

javascript - 选择 2 ajax : preselected data in edit mode

javascript - 循环遍历 Javascript 数组的索引时重置变量

Python:没有变量的类实例化的意义是什么

java - 调用从 main 方法扩展另一个类的类

javascript - 如何在表格中集成搜索功能?

javascript - 在初始化新日历之前,在引导选项卡上销毁 fullcalendar 单击