To tell you the truth, since the arrow function has been out, there is no need to worry about the this problem, and on the one hand, to simplify the code, the arrow function is loaded force artifact.
There are a few points to note in the arrow function, if the return value has only one row of expressions, you can omit return, which means that the row is the return value, otherwise you need to add a curly brace and return. If you have only one parameter, you can omit parentheses, and two need parentheses. For example, the following:
varF = v = v*2;//equivalent tovarf =function(v) {returnV*2;}//Judging even numbersvarIsEven = n = n% 2 = = 0;//need to add returnvar= (A, b) = = { if(A >=b)returnA; returnb;}
The This of the normal function is mutable, we classify the function into two states, one is the definition, the other is execution, and if careful study finds that the this in the function is always the object where the function executes. For example, when the global function executes, this executes the window, the object's method executes, and this is the variable of the function. And the This in the arrow function is fixed, see the following example:
function obj () { setTimeout (()=>console.log (this. id);} var id = 1// 2
The result of the execution is 2 instead of the global 1, indicating that when the SetTimeout function executes, this point is not the window, which differs from the normal function.
In fact, the arrow function does not have this object, and turning the arrow function into ES5 will find:
// ES6 function obj () { setTimeout (()=>console.log (this. id);} // ES5 function foo () { varthis; SetTimeout (function () { Console.log (this. ID); );}
Methods such as Call aply cannot bind the This in the arrow function:
var This . x; var x = 1// 1
A summary of this is to use the arrow function directly in the object's method, pointing to the window, the other arrow functions this will point to the previous layer of this, and the arrow function does not store this:
var obj = { 1, foo: ()== {returnthis . Id;} } var id = 2// 2
In addition to this, the arguments of the arrow function does not exist, cannot be constructed using new, and cannot use the yield command.
Arrow functions in the ES6