Mixins as class decorators in typescript/angular2
Danylo Mylian
638

Very cool, but it didn’t quite work for me. The fields were being left out as they weren’t set on the prototype. Here’s what I did…

export function mixIn(baseCtors: Function[]) {
return function (derivedCtor: Function) {
baseCtors.forEach((baseCtor) => {
const fieldCollector = {};
baseCtor.apply(fieldCollector);
Object.getOwnPropertyNames(fieldCollector).forEach((name) => {
derivedCtor.prototype[name] = fieldCollector[name];
})

Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});
};
}
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.