Ese6系列【结构赋值】

结构赋值:在日常开发中,我们经常会定义很多的数组或者对象,然后从数组或对象中提取出相关的信息,通常这样写代码会显得很凌乱,通过结构赋值可以大大简化了写代码的量。

下面我们来看一些例子来学习一下:

例子1:

const student = {
    name:'jsPool',
    age:20,
}
要取到name,和age的值,常规写法:
console.log(student.name)  //"jsPool"
console.log(student.age)  //20
es6写法
结构赋值
const {name,age}=student
console.log(name)//"jsPool"
console.log(age)//20

例子2:

const student = {
    scores:{
        math:95,
        chinese:98,
        english:93
    }
}
常规写法:
console.log(student.scores.math)//95
console.log(student.scores.chinese)//98
console.log(student.scores.english)//93
上面的常规写法,可以实现,但出现了好多重复的代码比如前面的student.scores,来看一下es6的写法,因为是
两层的花括号,通过解构赋值也是可以实现的
const {scores:{math,chinese,english}}=student
console.log(math)//95
console.log(chinese)//98
console.log(english)//93

上面是一些常规的解构赋值的方法,同样数组,混合类型都可以像上面那样进行解构赋值,今天就分享到这了,谢谢大家。

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值