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);
  };
};
相关推荐
2401_838472513 分钟前
C++中的装饰器模式实战
开发语言·c++·算法
沐知全栈开发10 分钟前
PHP 数组
开发语言
雨季66613 分钟前
Flutter 三端应用实战:OpenHarmony “心流之泉”——在碎片洪流中,为你筑一眼专注的清泉
开发语言·前端·flutter·交互
YMWM_25 分钟前
python3中类的__call__()方法介绍
开发语言·python
爱学习的阿磊27 分钟前
C++与Qt图形开发
开发语言·c++·算法
历程里程碑30 分钟前
Linux 16 环境变量
linux·运维·服务器·开发语言·数据库·c++·笔记
●VON32 分钟前
React Native for OpenHarmony:构建高性能、高体验的 TextInput 输入表单
javascript·学习·react native·react.js·von
cyforkk34 分钟前
15、Java 基础硬核复习:File类与IO流的核心逻辑与面试考点
java·开发语言·面试
●VON37 分钟前
React Native for OpenHarmony:ActivityIndicator 动画实现详解
javascript·学习·react native·react.js·性能优化·openharmony
空空潍40 分钟前
Python核心基础语法
开发语言·python