如何创建 1 或 2 个高阶函数/闭包来替代 class with 方法?
How can I create 1 or 2 higher order functions / closures as a alternative to a class with methods?
我正在尝试以一种很好的方式为名为 someFn
的函数创建高阶函数,我让它与 class 类型的
一起工作
const fn = (ctx, a, ...rest) => {};
class A {
constructor(ctx, opts) {
Object.assign(this, opts);
this.ctx = ctx;
}
someMethod(a, ...rest) {
return fn(this.ctx, a, ...rest);
}
}
const a = new A(ctx, opts);
someFn("something", (b) => a.someMethod.bind(a, b));
我想知道如何编写此代码并仅使用函数获得完全相同的行为,没有 class,并具有类似
的调用样式
someFn("something", withProxy(doThis))
您似乎正在寻找的高阶函数可能是
const fn = (ctx, a, ...rest) => {};
const fnCurried = (ctx) => (b) => (...rest) => fn(ctx, b, ...rest);
用作
someFn("something", fnCurried(ctx));
我正在尝试以一种很好的方式为名为 someFn
的函数创建高阶函数,我让它与 class 类型的
const fn = (ctx, a, ...rest) => {};
class A {
constructor(ctx, opts) {
Object.assign(this, opts);
this.ctx = ctx;
}
someMethod(a, ...rest) {
return fn(this.ctx, a, ...rest);
}
}
const a = new A(ctx, opts);
someFn("something", (b) => a.someMethod.bind(a, b));
我想知道如何编写此代码并仅使用函数获得完全相同的行为,没有 class,并具有类似
的调用样式someFn("something", withProxy(doThis))
您似乎正在寻找的高阶函数可能是
const fn = (ctx, a, ...rest) => {};
const fnCurried = (ctx) => (b) => (...rest) => fn(ctx, b, ...rest);
用作
someFn("something", fnCurried(ctx));