如何在JS中把一个方法包装在一个类构造函数中



我有一个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 () => {
// ...
});  

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

相关内容

最新更新