function
Arrow function
// variable and there is a direct return of var = F = V> V; // equivalent var = function F (V) { return V; }; // needs no arguments or return of a plurality of parameters not directly var = SUM (num1, num2) => { the let K = + num1 num2; return K; }; // equivalent var function = SUM (num1, num2) { the let K = + num1 num2; return K; };
this function problems arrow
Plug one call of use
var a = 10; function call_1() { console.log(this.a); } var call_obj = { a: 20 } call_1();//10 call_1.call(call_obj);//20 this指向call_obj
this function on the arrow did not understand ...... Do not believe your own summary
// function directly written by the little arrow can point call obj1.fn_new (); this execution window; var = TS 'global'; const = {OBJ1 TS: "OBJ1", fn_new: () => { Console .log (this.ts); } } obj1.fn_new (); // global // bit regardless of how many layers can directly point to point is that this window const = {OBJ3 TS: "OBJ3", fn_obj: { TS: " obj3_3 ", fn_new_2: () => { the console.log (this.ts); } } } obj3.fn_obj.fn_new_2 () // global
// If the arrow is only obtained after a function used () diao call. var ts = 'global'; const obj2 = { TS: "obj2", fn_new_1: function () { the console.log (this.ts); return () => { the console.log (this.ts); } } } let fn = obj2.fn_new_1 (); // obj2 fn is a function of the arrow by () call before they get the Fn (); // obj2 // arrow function of this generation with the function of the arrow function (here is fn_new_1 function) this refers to the same // this fn_new_1 here is a function call directed to its target fn_obj // call this change also followed when the function becomes an arrow const = {OBJ3 TS: "OBJ3", fn_obj: { TS: "obj3_3", fn_new_1 : function () { the console.log (this.ts); return () => { the console.log (this.ts); } } } } Let fnn = obj3.fn_obj.fn_new_1 (); // obj3_3 fnn (); // obj3_3
Some magic
// arrow arrow this function itself is not a function of this inner layer is equal to the multiple nested outer this var A = 2; var K = { A:. 1, FF: function () { the console.log (this.a) return () => { the console.log (this.a); return () => { the console.log (this.a); } } } } the let k.ff F1 = (); //. 1 the let F2 = F1 (); //. 1 F2 (); //. 1
// use settimeout feeling is not right in front of the ideas regardless of the timer on the right // call the function foo only when the timer function will start to take effect arrow // arrow with the function of this function to generate arrow function (here is a function foo) of this same point var ID = 10; function foo () { the console.log (this.id); // 10 the setTimeout (() => { the console.log ( 'ID:', this.id) ; }, 100); } foo (); // foo 10 points of this window (because window.foo ()) var KK = { ID: 20 is, Fuu: function () { the setTimeout (() => { Console. log ( 'ID:', this.id); }, 100); } } kk.fuu (); // Fuu 20 is a function of this arrow will point to point kk kk
arrow function arguments
// Get argument function FUNC (A, B, C) { the console.log (arguments); // [. 1, 2,. 3, the callee: ƒ, the Symbol (Symbol.iterator): ƒ] the console.log (arguments [ . 4]); // undefined the console.log (arguments [. 3]); //. 4 } FUNC (1,2,3,4) //...a result of a so unlock equal to 2,3 equal to [l, 2,3] the let Fn = (... a) => { the console.log (a); } Fn (l, 2,3); // [. 1, 2,. 3]