ES6的箭头函数,参数扩展以及解构赋值

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)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值