Search code examples
javascriptscopethisarrow-functions

Is this an error to have 'a' as the input for this arrow function?


From You don't know JS - this & Object prototypes, by Kyle Simpson, page 32, heading Lexical this:

function foo() {
    return (a) => {
        console.log(this.a);
    };
}

var obj1 = {
    a:2
}

var obj2 = {
    a:3
}

var bar = foo.call(obj1);
bar.call(obj2); // 2, not 3!

The point here is that this returned arrow function will inherit the lexical this from function foo.

What I do not understand is why the input for this arrow function above is a?

  • I removed the 'a' from the arrow function and this example still works as expected.
  • When the arrow function is called via references, they are called with no input arguments.

What is the point of having 'a' as the input to the arrow function?


Solution

  • What is the point of having 'a' as the input to the arrow function?

    As you said, it is a variable declaration that is never used.

    It is pointless.

    ESLint reports:

    'a' is defined but never used. (no-unused-vars)