typescript TypeScript Class Mixin

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript TypeScript Class Mixin相关的知识,希望对你有一定的参考价值。

class Disposal { 
    public isDisposal: boolean = false;

    public active(): void { 
        this.isDisposal = true;
    }
}

class Active {
    public isActive: boolean = false;

    public activate(): void { 
        this.isActive = true;
    }

    public deactivate(): void { 
        this.isActive = false;
    }
}

class SuperClass implements Disposal, Active { 
    /** Disposal Interface */
    public isDisposal: boolean = false;
    public active: () => void;

    /** Active Interface */
    public isActive: boolean = false;
    public activate: () => void;
    public deactivate: () => void;

    public isActivated(): boolean { 
        return this.isActive;
    }

    public isDisposaled(): boolean { 
        return this.isDisposal;
    }
}
mixin(SuperClass, [Disposal, Active]);

function mixin(baseClass: any, implClasses: any[]) { 
    implClasses.forEach(implClass => {
        Object.getOwnPropertyNames(implClass.prototype).forEach((property: string) => {
            console.log(implClass.prototype);
            baseClass.prototype[property] = implClass.prototype[property];
        });
    });
}

window['supa'] = new SuperClass();

以上是关于typescript TypeScript Class Mixin的主要内容,如果未能解决你的问题,请参考以下文章

TypeScript入门学习之路

typescript TypeScript Snippets #typescript

TypeScript入门五:TypeScript的接口

TypeScript系列教程--初探TypeScript

TypeScript入门三:TypeScript函数类型

typescript使用 TypeScript 开发 Vue 组件