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);
  };
};
相关推荐
清水白石0086 分钟前
Python 编程实战全景:从基础语法到插件架构、异步性能与工程最佳实践
开发语言·python·架构
Mh1 小时前
我决定写一个 3D 地球仪来记录下我要去的地方
前端·javascript·动效
. . . . .2 小时前
ref、useRef 和 forwardRef
前端·javascript·react.js
Halo_tjn2 小时前
Java 基于字符串相关知识点
java·开发语言·算法
梦想的颜色3 小时前
java 利用redis来限制用户频繁点击
java·开发语言
报错小能手3 小时前
Swift 并发 Combine响应式框架
开发语言·ios·swift
柚子8163 小时前
break跳出语句块的神奇技巧
前端·javascript
万法若空3 小时前
C++ <memory> 库全方位详解
开发语言·c++
代码中介商3 小时前
C++ 类型转换深度解析:static_cast、dynamic_cast、const_cast、reinterpret_cast
开发语言·c++
青小莫3 小时前
C++之string(OJ练习)
开发语言·c++·stl