MST
星途 面试题库

面试题:TypeScript类装饰器的基本使用

请用TypeScript编写一个类装饰器,用于记录类实例化的时间,并在控制台输出。假设类名为`MyClass`,请给出完整代码。
27.5万 热度难度
前端开发TypeScript

知识考点

AI 面试

面试题答案

一键面试
function logInstantiationTime(target: Function) {
    return class extends target {
        constructor(...args: any[]) {
            super(...args);
            console.log(`Instance of ${target.name} created at ${new Date().toISOString()}`);
        }
    };
}

@logInstantiationTime
class MyClass {
    // 类的具体实现可以在这里添加
}

// 测试实例化
const myInstance = new MyClass();