首页 > MDN中的一个继承多个对象的DEMO问题

MDN中的一个继承多个对象的DEMO问题

function MyClass() {
     SuperClass.call(this);
     OtherSuperClass.call(this);
}

MyClass.prototype = Object.create(SuperClass.prototype); //inherit
mixin(MyClass.prototype, OtherSuperClass.prototype); //mixin

MyClass.prototype.myMethod = function() {
     // do a thing
};

这个代码 mixin不是很懂


JavaScript是Prototype-based的OOP语言,通常有两大类继承模式:

伪类模式 Pseudo-Class Mode

伪类模式利用原型链的特性来重用对象成员。

混合模式 mixin

混合模式即是将一个对象的成员直接拷贝到另一个对象上,这种模式并不会涉及原型链。

二者的区别:

之前总结过一篇文章: https://github.com/leozdgao/personal-utils/wiki/Inheritance-in-JavaScr...


Mixin是JavaScript中用的最普遍的模式,几乎所有流行类库都会有Mixin的实现。Mixin是掺合,混合,糅合的意思,即可以就任意一个对象的全部或部分属性拷贝到另一个对象上。
mixin(MyClass.prototype,OtherSuperClass.prot
otype); 就是将OtherSuperClass的原型拷贝到MyClass的原型上。

【热门文章】
【热门文章】