我试图和朋友一起解决这个问题。
假设您有这样的函数:
const foo = function(i) { return function(j){ return 2 * j } };
我如何用不同的方法覆盖foo
?
假设我想重用foo
,使用不同的匿名函数来执行类似function(j){ return 3 * j; };
的操作
我该怎么办?
编辑:我看到this post,但这是变量。我正在特别询问匿名方法。
编辑2:这是一个概念证明,所以让我们说foo
是一个const
我不能把它变成一个let
const constantFunction = x => x + 1;
let variableFunction = x => x + 2;
try {
variableFunction = x => x + 3;
console.log('Re-assigning to a variable: DONE');
} catch(e) {
console.log('Error while re-assigning to a variable');
}
try {
constantFunction = x => x + 3;
console.log('Re-assigning to a constant: DONE');
} catch(e) {
console.log('Error while re-assigning to a constant');
}