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);
  };
};
相关推荐
吴声子夜歌1 分钟前
ES6——数组的扩展详解
前端·javascript·es6
WangJunXiang68 分钟前
Python网络编程
开发语言·网络·python
guhy fighting10 分钟前
new Map,Array.from,Object.entries的作用以及使用方法
开发语言·前端·javascript
lsx20240610 分钟前
操作系统统计
开发语言
_下雨天.15 分钟前
Python 网络编程
开发语言·网络·python
小樱花的樱花17 分钟前
打造高效记事本:UI设计到功能实现
开发语言·c++·qt·ui
橘子编程32 分钟前
MindOS:你的AI第二大脑知识库
java·开发语言·人工智能·计算机网络·ai
aini_lovee38 分钟前
C# 快速搜索磁盘文件解决方案
开发语言·c#
小陈工39 分钟前
2026年4月8日技术资讯洞察:边缘AI推理框架竞争白热化,Python后端开发者的机遇与挑战
开发语言·数据库·人工智能·python·微服务·回归
零二年的冬44 分钟前
epoll详解
java·linux·开发语言·c++·链表