Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why this and null is interchangeable here and where this actually point to? JavaScript Bind and Apply

I got a little confused with JS bind, apply, and this. Does this here actually 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)

curriedJoin(1, 2, 3) // '1_2_3'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading