The value of "this" in an arrow function

In arrow functions, the value of the this keyword is the same as the value of the this keyword of the enclosing scope (the global or function scope, whichever the arrow function is defined inside). That means, instead of referring to the context object (that is, the object inside which the function is a property), which is the value of this in traditional functions, this instead refers to global or function scope, in which the function is called. Consider this example to understand the difference between the traditional functions and the arrow functions, this value:

var car = {  name: 'Bugatti',  fuel: 0,  // site A  addFuel: function() {             // site B             setInterval(function() {              // site C              this.fuel++; console.log("The ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.