箭头函数

79 阅读1分钟

箭头函数

  • 就是ES6 对普通函数 写法上的优化

语法

    1. const 函数名 = (形参) =>{函数体} : 其中如果书写时参数只有一个时可以不写小括号 () 除此之外必写
const fn = a => {console.log(a)} fn(100)   //只有一个参数可以不写这个小括号()

const fn1 = (b, c) =>{console.log(a+b)} fn1(100, 200)// 多个形参时小括号必写
    1. 函数体只有一行代码时 , 可以省略大括号 {},并且不用书写 return 函数会自动返回值
const fn2 = (a, b) => a+b 
let sum = fn2(100, 200)
console.log(sum)