【问题标题】:How do you wrap a method inside a Class Constructor in JS如何在 JS 中的类构造函数中包装方法
【发布时间】:2021-10-19 00:10:52
【问题描述】:

我有一个 Class 构造函数,我想用一个函数包装其中一个方法。这样做的语法是什么?

// function that wraps around the method
const wrapCatch = (fn) => async (...args) =>
  fn(args).catch(err => throw new Error(err));

class myClass {
  constructor(fn1) {
    this.fn1 = fn1;
  }

  async fnToBeWrapped () {
    return await this.fn1();
  }
}

基本上是在尝试这样做,但对于类方法...

const fnToBeWrapped = wrapCatch(async () => {
  // ...
});  

【问题讨论】:

  • 仅供参考 fn(args) 应该是 fn(...args)

标签: javascript class methods constructor


【解决方案1】:

在 Java 中不能将方法包装在类中

【讨论】:

    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 2012-04-23
    • 2015-10-17
    • 2011-02-22
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多