面试题答案
一键面试// 定义Logger混入
type Constructor<T = {}> = new (...args: any[]) => T;
function Logger<TBase extends Constructor>(Base: TBase) {
return class extends Base {
log() {
console.log(`Instance of ${this.constructor.name}`);
}
};
}
// 使用Logger混入
class MyClass {
// 原始类的内容
}
const MyClassWithLogger = Logger(MyClass);
const instance = new MyClassWithLogger();
instance.log();