// Shamelessly copied code from MDN added with loggingfunctionPerson(){this.age=0;setInterval(()=>{console.log(this.age++);// |this| properly refers to the Person object},1000);}varp=newPerson();
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).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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!
In JavaScript's case, it helps with some this issues.
The callback here in
setInterval
works fine and well (logging0,1,2
etc.), yet without using arrow functions (i.e. before ES6), the callback would look like:The
this
in the callback function refers toWindow
(loggingNaN
) 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).