Technically, JavaScript does not offer multiple inheritance. Each object has a well-defined single "prototype" object, and thus a "prototype chain".
However, it is possible to augment any object with additional methods, so-called "expandos". So you could iterate over a collection of methods and individually add them to newly created objects. Such a collection is called "mixin".
Several frameworks offer mixins, for example:
- qooxdoo
- ExtJS
- mootools
- ...
They all work pretty much the same.
Note however that this is not real inheritance, since changes to the mixin will not be reflected in the objects.
For example:
var mixin = {
method: function () {
console.log('Hello world!');
}
};
var foo = new fun1();
foo.method = mixin.method;
foo.method(); // Hello world!
mixin.method = function () { console.log('I changed!') };
foo.method(); // Hello world!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…