+ 2

When need to use arrow function?

arrow function in es6

5th Nov 2016, 6:15 AM
Вадим Глушков
Вадим Глушков - avatar
2 ответов
+ 2
Much of the time, you don't need to use it, it is primarily there for convenience in throwaway callback functions and reduces boilerplate noise in your code. For example, `[1,2,3].map(v => v * 2)` instead of `[1,2,3].map(function(v) { return v * 2; })`. Importantly, it also fixes an issue with `this`; because the scoping works differently, it allows you to use `this` from the outer scope, which means you don't ever have to use `var self = this;` or var myTimeout = function(){ blah blah blah }.bind(this);` to hold onto the value. That's the most important practical reason.
5th Nov 2016, 12:39 PM
Daniel Couper
Daniel Couper - avatar
+ 1
Daniel! Thank you. Very excellent answer.
5th Nov 2016, 4:52 PM
Вадим Глушков
Вадим Глушков - avatar