참고

설명

예제

class Disposable {
    isDisposed! : boolean;
    dispose() {
        this.isDisposed = true;
        console.log("Set disposed.");
    } 
}

class Activatable {
    isActive! : boolean;
    activate() {
        this.isActive = true;
        console.log("Set activated.");
    }
    deactivate() {
        this.isActive = false;
        console.log("Set deactivated.");
    }
}

interface SmartObject extends Disposable, Activatable {}
class SmartObject {
    constructor() {}
}

function applyMixins(derivedCtor : any , baseCtors: any[]) {
    baseCtors.forEach((baseCtor) => {
        Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
            Object.defineProperty(
                derivedCtor.prototype,
                name,
                Object.getOwnPropertyDescriptor(baseCtor.prototype, name) || Object.create(null)
            );
        })  
    })
}

applyMixins(SmartObject, [Disposable , Activatable]);
let smartObj = new SmartObject();
smartObj.activate();
smartObj.deactivate();
smartObj.dispose();