Created
March 10, 2018 17:13
-
-
Save Soul-Master/d3076faaa6dc292ab44b97549de77ea8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
It's impossible to call constructor of mixin class. All default properties in TypeScript don't work. | |
*/ | |
function applyMixins<T>(derivedCtor: any, ...baseCtors: any[]) { | |
baseCtors.forEach(baseCtor => { | |
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => { | |
derivedCtor.prototype[name] = baseCtor.prototype[name]; | |
}); | |
}); | |
return <Constructor<T>>derivedCtor; | |
} | |
export class BaseFeature { | |
width = 100; | |
height = 100; | |
resize(percent: number) { | |
console.log('resize to: ' + percent); | |
return true; | |
} | |
} | |
export abstract class ClosableObject { | |
isClosed = false; | |
close() { | |
console.log('close object'); | |
} | |
} | |
export abstract class MovableObject { | |
isMovable = true; | |
move(top: number, left: number) { | |
console.log('move to: ' + top + ', ' + left); | |
} | |
} | |
export interface FullFeature extends ClosableObject, MovableObject { } | |
export class FullFeatureObject extends applyMixins<FullFeature>(ClosableObject, MovableObject) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment