当前位置:网站首页>JS arrow function

JS arrow function

2022-06-25 04:24:00 Robin Luo Bing

The syntax of arrow function expression is more concise than function expression , And there's no one of its own this,arguments,super or new.target. Arrow function expressions are more suitable for places where anonymous functions are needed , And it cannot be used as a constructor .

1、 Basic grammar

(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression
// amount to :(param1, param2, …, paramN) =>{ return expression; }

//  When there is only one parameter , Parentheses are optional :
(singleParam) => { statements }
singleParam => { statements }

//  Functions without arguments should be written as a pair of parentheses .
() => { statements }

2、 Advanced Grammar

// The bracketed function body returns the object literal expression :
params => ({foo: bar})

// The remaining parameters and default parameters are supported 
(param1, param2, ...rest) => { statements }
(param1 = defaultValue1, param2, …, paramN = defaultValueN) => {
statements }

// Parameter list deconstruction is also supported 
let f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;
f();  // 6

 

原网站

版权声明
本文为[Robin Luo Bing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210534569674.html