I got a little confused with JS bind, apply, and this.
Questions
this and null interchangeable in the following snippet?this in the following context point to the window?function curry(fn) {
// your code here
return function curryInner(...args) {
if (args.length >= fn.length) return fn.apply(this, args);
return curryInner.bind(this, ...args); //change this to null, still pass the test
};
}
const join = (a, b, c) => {
return `${a}_${b}_${c}`
}
const curriedJoin = curry(join)
console.log(curriedJoin(1, 2, 3)) // '1_2_3'
No, this points to the this context of the function it is in.
In this case, it really doesn't matter what you put there since the only purpose of calling bind is to create a copy of the function with the args already set.
return curryInner.bind(null, ...args)
could essentially be replaced with
return function() {
return curryInner(args);
}