Javascript
Arrow functions
TLDR
- More concise
- Implicit return
this
picked up from context
Implicit return
No need to specify a return for simple functions, it is already there implicitly.
const double = (x) => x * 2;
console.log(double(2)); // 4
this from context
this
is immediately picked up from the parent/usage context, eliminating old need to use old this assignment trick.
No more
this.myVar = 0;
var that = this;
function() {
that.myVar++;
}
Now
this.myVar = 0;
() => {
this.myVar ++;
}
Extra
Note: No need to use brackets if only one argument.
const double = x => x * 2;