ES6系列二:函数

一、箭头函数,就是函数的简写。
 如果只有一个参数,() 可以省
 如果只有一个return,{}可以省

// 普通函数
function name() {

}
// 箭头函数,去掉 function, 加上 =>
() => {

}
let show1 = function () {
    console.log('abc')
}
let show2 = () => {
    console.log('abc')
}
show1()
show2()

let show4 = function (a) {
    return a*2
}
let show5 = a => a * 2   //只有一个参数,省略() 。只有一个return,省略{}
console.log(show4(10))
console.log(show5(10))

二、函数的参数
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 arr=[1,2,3];
function show(a,b,c) {
    alert(a);
    alert(b);
    alert(c);
}
show(...arr);  //相当于show(1,2,3)

let arr1 = [1, 2, 3]
let arr2 = [4, 5, 6]
let arr3 = [...arr1, ...arr2]
console.log(arr3)

//展开参数
function show(...args) {
    fn(...args);
}
function fn(a,b) {
    alert(a+b);
}
show(12,5);

//默认参数
function show2(a, b=5, c=8) {
    console.log(a, b, c)
}
show2(88, 12)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值