DEV Community

[Comment from a deleted post]
Collapse
 
mogery profile image
Gergő Móricz • Edited

Yeah, I know about that, and it is a thing that boosts how fast you can write code IMO. If that's wrong in your opinion, tell me!

Collapse
 
ytjchan profile image
ytjchan • Edited

In JavaScript's case, it helps with some this issues.

// Shamelessly copied code from MDN added with logging
function Person(){
  this.age = 0;

  setInterval(() => {
    console.log(this.age++); // |this| properly refers to the Person object
  }, 1000);
}

var p = new Person();

The callback here in setInterval works fine and well (logging 0,1,2 etc.), yet without using arrow functions (i.e. before ES6), the callback would look like:

setInterval(function() {
  console.log(this.age++); // JS be like, "What's this?"
}, 1000);

The this in the callback function refers to Window (logging NaN) and all hell breaks loose.

Your point of writing faster is true, but it has other benefits depending on the language too 😉 (in Java's case, a more compact code and easier code tracing, plus introducing functional programming to its OOP world).