Skip to content

Instantly share code, notes, and snippets.

@beholderrk
Created February 10, 2015 15:17
Show Gist options
  • Save beholderrk/ee7ca84882620d76aee6 to your computer and use it in GitHub Desktop.
Save beholderrk/ee7ca84882620d76aee6 to your computer and use it in GitHub Desktop.
mixins demonstration in typescript
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});
}
class ConstructorMixin {
c: number;
setC(c: number) {
this.c = c;
}
}
class AnotherMethodMixin {
callMe(): number {
return 123;
}
}
class First implements ConstructorMixin, AnotherMethodMixin {
a: number;
b: number;
c: number;
setC: (c: number) => void;
callMe: () => number;
constructor() {
this.a = 1;
this.b = 2;
this.setC(3);
}
}
applyMixins(First, [ConstructorMixin, AnotherMethodMixin]);
ddescribe('test mixins', () => {
it('', () => {
var f = new First();
expect(f.a).toBe(1);
expect(f.b).toBe(2);
expect(f.c).toBe(3);
expect(f.callMe()).toBe(123);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment