-1

I got a little confused with JS bind, apply, and this.

Questions

  • Why are this and null interchangeable in the following snippet?
  • Does 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'
OldBuildingAndLoan
  • 2,801
  • 4
  • 32
  • 40
bbcts
  • 25
  • 3

1 Answers1

2

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);
}
skara9
  • 4,042
  • 1
  • 6
  • 21