ES6 箭头函数

1. 若函数体只有一句代码,且代码执行结果为返回值,则函数体大括号以及return可省略

正常函数形式:

let normal_func = function (num1, num2) {
    return num1 + num2;
  };

使用箭头函数形式:

let arrow_func = (num1, num2) => num1 + num2;
console.log(normal_func(15, 15)); // 30
console.log(arrow_func(15, 20)); // 35

2. 若函数命名参数只有一个,则可以省略小括号

let arrow_func = num1 => num1;

3. 若函数无命名参数,则小括号不能省略

let arrow_func = () => console.log("Hello Arrow_Function.");

 

posted @ 2021-08-09 17:10  TwinkleG  Views(53)  Comments(0)    收藏  举报