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);
  };
};
相关推荐
Hilaku42 分钟前
2026 年了,jQuery 真的该死了吗?
前端·javascript·程序员
htzyl2061 小时前
javaScript——this 的四种绑定:为什么箭头函数改不了它
前端·javascript
bai2731 小时前
*.js 匹配到了 B.JS:fs.globSync 在 Windows 上的五个默认值
javascript·后端
geovindu1 小时前
python: Decision Tree
开发语言·后端·python·决策树
时间的情敌2 小时前
Three.js 工业场景性能优化合集|大批量管线、InstancedMesh 实例化、合并 Geometry、显存释放
开发语言·javascript·性能优化·three.js
Sakura_j_h_2 小时前
TypeScript 7.0 换 Go 编译器后,我先在 6.0 清完默认值
javascript·golang·typescript·vite·前端工程化·tsc
用户298698530142 小时前
在 React 中实现 Excel 与 TXT 格式互转的前端方案
javascript·react.js·excel
步峰代理-盾山3 小时前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫
map_3d_vis3 小时前
百度地图地图类型切换:普通地图、卫星图、混合图
前端·javascript·地图·百度地图
sharemylove13 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习