This | JS/ES

Arrow Function#

Arrow functions don’t bind their own this value. Instead, the this value of the scope in which it was defined is accessible.

For methods, ES6 provides a new method definition syntax. You can see this in the definition of the printGuestList method below. That function is a standard function, just with a shorthand syntax which allows for the removal of the colon and the function keyword.

Because arrow functions don’t bind this, they work well for everything except methods. As shown below, the arrow function passed to forEach is able to access this.name correctly, as it’s defined as an arrow function and doesn’t have a this binding of its own. That code wouldn’t work if you swapped out the arrow function for a standard function.

const event = {
name: "Birthday Party",
guestList: ["Andrew", "Jen", "Mike"],
printGuestList() {
console.log("Guest list for " + this.name);
this.guestList.forEach((guest) => {
console.log(guest + " is attending " + this.name);
});
},
};
event.printGuestList();