在Javascript中实现多重继承 [英] Implement multiple inheritance in Javascript

查看:105
本文介绍了在Javascript中实现多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察到通过在Javascript中使用prototype属性(并间接设置原型链),可以在JavaScript中实现多级继承。但是有可能通过某种方式在Javascript中实现多重继承吗?任何简单的例子都会很棒。

I have observed that through the use of "prototype" property in Javascript (and indirectly setting up the prototype chain), one can implement multi-level inheritance in JavaScript. But is it possible to implement multiple inheritance in Javascript by some way? Any simple examples would be great.

推荐答案

要实现简单的继承,你通常会这样做

To implement simple inheritance you usually do

MyClass.prototype = new MySuperClass();

但你也可以复制另一个类的内容:

but you could also copy the content of another "class" :

MyClass.prototype = new MySuperClass();
var myOtherSuperClass = new MyOtherSuperClass();
for (var key in myOtherSuperClass) {
     MyClass.prototype[key] = myOtherSuperClass[key];
}

当然你可以使用像 jQuery.extend 这样做,或自己动手。

Of course you could use an utility like jQuery.extend to do that, or roll your own.

A限制是 instanceof 将无法检测到 MyOtherSuperClass

A limit is that instanceof won't detect MyOtherSuperClass.

这篇关于在Javascript中实现多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆