Backbencher.dev

JS Daily 8 - Using apply() With Arrow Functions

Last updated on 28 Nov, 2020

What is the output of following code?

const f = () => {
  console.log(this.a);
}
f.apply({ a: 10 });
----o----

Output is undefined.

In case of arrow function, this is not bound to the function. This rule is applicable even when used with call() or apply().

In our example, f() is defined in the global scope(window). Therefore in line 2, when this.a is printed, it prints the value of window.a. Since, there is no global variable a, undefined is printed.

--- ○ ---
Joby Joseph
Web Architect