1、箭头函数,就是函数的简写
①如果只有一个参数,() 可以省
②如果只有一个return,{}可以省
// 普通函数
function name() {
}
// 箭头函数,去掉 function, 加上 =>
() => {
}
let show1 = function () {
console.log('abc')
}
let show2 = () => {
console.log('abc')
}
show1() // 调用函数 abc
show2() // abc
let show4 = function (a) {
return a*2
}
let show5 = a => a * 2 //简洁,类似python lambda 函数
console.log(show4(10))
console.log(show5(10))
// 打印结果 20
// 20
2、函数 —— 参数
(1)参数扩展/展开 …args
①收集剩余的参数,必须当到最后一个参数位置
②展开数组,简写,效果和直接把数组的内容写在这儿一样
(2)默认参数
function show(a, b, ...args) {
console.log(a)
console.log(b)
console.log(args)
}
console.log(show(1, 2, 3, 4, 5))
let arr1 = [1, 2, 3]
let arr2 = [4, 5, 6]
let arr3 = [...arr1, ...arr2]
console.log(arr3)
function show2(a, b=5, c=8) {
console.log(a, b, c)
}
show2(88, 12)
3、解构赋值
①左右两个边结构必须一样
②右边必须是个东西
③声明和赋值赋值不能分开,必须在一句话里
let [a, b, c] = [1, 2, 3]
console.log(a, b, c)
let {x, y, z} = {x: 1, y: 2, z: 3}
console.log(x, y, z)
let [json, arr, num, str] = [{ a: 1, b: 2 }, [1, 2, 3], 8, 'str']
console.log(json, arr, num, str)