ES6 A new function is added to the standard :Arrow Function( Arrow function )
x => x * x
amount to :
function (x) { return x * x; }
Arrow function is equivalent to anonymous function , And simplify the function definition . There are two formats for arrow functions , A kind of , Contains only one expression , even { …
} and return It's all omitted . There is also one that can contain multiple statements , In this case, we can't omit it { … } and return:
x => { if (x > 0) { return x * x; } else { return - x * x; } }
If the parameter is not one , You need brackets () Put it all together :
// Two parameters : (x, y) => x * x + y * y // No parameters : () => 3.14 // Variable parameters : (x, y, ...rest) =>
{ var i, sum = x + y; for (i=0; i<rest.length; i++) { sum += rest[i]; } return
sum; }
If you want to return an object , Just pay attention , If it's a single expression , If you write like that, you'll make a mistake :
// SyntaxError: x => { foo: x }
Because of the { … } There is a grammar conflict , So we need to change it to :
// ok: x => ({ foo: x })
this

Arrow function seems to be a shorthand for anonymous function , But actually , There is an obvious difference between arrow function and anonymous function : Inside the arrow function this Is lexical scope , Determined by context .

Review previous examples , because JavaScript Function pair this Binding error handling , The following example can't get the expected result :
var obj = { birth: 1990, getAge: function () { var b = this.birth; // 1990 var
fn = function () { return new Date().getFullYear() - this.birth; //
this point window or undefined }; return fn(); } };
Now? , Arrow function completely fixed this Direction of ,this Always point to lexical scope , That is, the outer layer caller obj:
var obj = { birth: 1990, getAge: function () { var b = this.birth; // 1990 var
fn = () => new Date().getFullYear() - this.birth; // this point obj object return fn(); }
}; obj.getAge(); // 25
If you use the arrow function , The old one hack Writing method :
var that = this;
It's no longer needed .

because this Bound to lexical scope in arrow function , therefore , use call() perhaps apply() When calling arrow function , Can't this Binding , That is, the first parameter passed in is ignored :
var obj = { birth: 1990, getAge: function (year) { var b = this.birth; // 1990
var fn = (y) => y - this.birth; // this.birth still 1990 return fn.call({birth:2000},
year); } }; obj.getAge(2015); // 25

Technology
©2019-2020 Toolsou All rights reserved,
python read , write in txt Text content ajax get Request Chinese parameter garbled solution Keras Summary of training data loading rk3399_android7.1 debugging USB Summary of Bluetooth module LED Scrolling text inherit jpa Repository Write custom method query springboot Multi profile configuration experiment 11-1-6 Output string at specified position (20 branch )( Essence )2020 year 7 month 21 day ASP.NET Core Use of global filters org.postgresql.util.PSQLException Processing records