ES6 函数参数(默认参数、结构)

重来没有系统的学过es6,感觉语法和swift 、java、python等语言差不多,今天给我上了一课。

 

缘起


今天看了一个函数,愣是看的有点蒙。


clone ({ zindex = this.commonStyle.zindex + 1 } = {}) {

}

最后终于弄明白了,写点总结

 

ES6函数参数


1、默认值

这个没什么说的,就是传入undefine 的时候使用默认值,注意null不行。


function esFn(name = 'zzy') {
  console.log(name);
}
esFn()
esFn(undefined)
esFn(null)
esFn('zzzzy')

查看运行结果:

[Running] node "/Users/mac/Desktop/work/low-code/luban-h5/front-end/h5/src/components/core/models/tempCodeRunnerFile.js"
zzy
zzy
null
zzzzy

2、结构


function test({name,age = 18}) {
  console.log(name);
  console.log(age);
}


let p = {
  name:'zy'
}

test(p)

查看运行结果:

[Running] node "/Users/mac/Desktop/work/low-code/luban-h5/front-end/h5/src/components/core/models/tempCodeRunnerFile.js"
zy
18

就是对于对象等复杂结构结构传入,看代码即可。

3、无限参数

大部分语言都支持


function wuxianP(...params) {
  console.log(params);
}

wuxianP(1)
wuxianP(1,'1')
wuxianP(1,'1',[1,1])

查看运行结果:

[Running] node "/Users/mac/Desktop/work/low-code/luban-h5/front-end/h5/src/components/core/models/tempCodeRunnerFile.js"
[ 1 ]
[ 1, '1' ]
[ 1, '1', [ 1, 1 ] ]

 

缘落


// 传入对象
//默认值是{}
//结构的默认值zindex 不传默认值this.commonStyle.zindex + 1
clone ({ zindex = this.commonStyle.zindex + 1 } = {}) {

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星宇大前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值