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);
  };
};
相关推荐
佩奇大王16 分钟前
P2408 特殊日期
java·开发语言
YMH.18 分钟前
Day3.14c++
开发语言·c++
花间相见23 分钟前
【JAVA基础11】—— 吃透原码、反码、补码:计算机数值表示的底层逻辑
java·开发语言·笔记
han_23 分钟前
前端性能优化之白屏、卡顿指标和网络环境采集篇
前端·javascript·性能优化
阿蒙Amon24 分钟前
C#常用类库-详解Playwright
开发语言·c#
特种加菲猫24 分钟前
C++ std::list 完全指南:从入门到精通所有接口
开发语言·c++
清空mega25 分钟前
第4章:JSP 程序设计实战——for、if、动态表格与 99 乘法表
开发语言·python
共享家952730 分钟前
Java入门(类和对象)
java·开发语言
习惯就好zz35 分钟前
Qt Quick 系统托盘完整实践
开发语言·qt·qml·系统托盘·system tray·qapplication·qguiapplication