ts装饰器保存this指向

要使用 function 而不是 箭头函数

typescript 复制代码
interface MethodDecorator<T> {
  (
    target: Object,
    propertyKey: string | symbol,
    descriptor: TypedPropertyDescriptor<T>
  ): TypedPropertyDescriptor<T> | void;
}

type TMethodDecorator = MethodDecorator<any>;

const matchImport: TMethodDecorator = (target, propertyKey, descriptor) => {
  const callback = descriptor.value;

  descriptor.value = function (...args: any[]) {
    console.log("decorator", this);
    return callback.call(this, ...args);
  };
};
相关推荐
2403_83556847几秒前
C++代码规范化工具
开发语言·c++·算法
山上三树25 分钟前
Qt Widget介绍
开发语言·qt
weixin_387534221 小时前
Ownership - Rust Hardcore Head to Toe
开发语言·后端·算法·rust
小J听不清1 小时前
CSS 边框(border)全解析:样式 / 宽度 / 颜色 / 方向取值
前端·javascript·css·html·css3
csdn_zhangchunfeng2 小时前
Qt之slots和Q_SLOTS的区别
开发语言·qt
计算机安禾2 小时前
【C语言程序设计】第35篇:文件的打开、关闭与读写操作
c语言·开发语言·c++·vscode·算法·visual studio code·visual studio
kishu_iOS&AI2 小时前
Python - 链表浅析
开发语言·python·链表
用户15815963743702 小时前
多 Agent 系统容错与恢复机制:OAuth 过期、Cron 级联失败的工程解法
javascript
m0_733612212 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
luffy54592 小时前
Rust语言入门-变量篇
开发语言·后端·rust