Arrow function (Arrow Functions)
A more concise wording
1. The original wording
function method(params){ return params*2; }
2. Functions arrow
(params) => {params*2;}
Get a line.
a. If only one parameter, the brackets can be omitted
params => {params*2;}
b. If only one expression, braces can be omitted
params => params*2;
Second, this point will not change
Example: Implementing a ++
var Method = function(){ this.a = 0; this.timer = setInterval(function(){ this.a++; console.log(this.a); },1000); }
var method = new Method();
Output:
NaN
NaN
NaN
...
Because setInterval function, this, pointed windows, windows in a defined, you can not output the desired value.
Modify arrow function can be achieved a ++;
var Method = function(){ this.a = 0; this.timer = setInterval(() => { this.a++; console.log(this.a); }) } var method = new Method();
Output:
1 2 3 ...