let fn = (a, b, c) => {
console.log(a, b, c)
}
fn1(0, 0, 0) // output: 0 0 0
I want fn to always add 2 to the second parameter every time it is called
Right now
fn(0, 0, 0) // output: 0 2 0
fn(1, 1, 1) // output: 1 3 1
Currently I have only found a very ugly way to write hijack:
fn = (_ => {
const innerFn = fn
const newFn = (a, b, c) => {
innerFn(a, b + 2, c)
}
Object.assign(newFn, innerFn)
return newFn
})()
Is there a better packaging method?
The method is correct, but I always feel that your writing is a bit awkward... I think it is better to be more direct...
Uh-huh. .
In fact, it is nothing more than adding 0, 2, 0 to the parameters respectively
That is to say, another
is generated from function fn偏函数
fnOffsetAdd the three parameters [0, 2, 0] to a b c on fn(a, b, c) respectively
In a broader sense:
Place
[ .... ]
这n
个参数 分别加到fn()
的arguments
at the corresponding positionIt should be more elegant to use
fn020
as variable name = =What you describe is a bit like ES6’s Proxy, but this cannot be polyfilled and may not be suitable for use on the front end.