ES(2)变量解构

数组的解构赋值


var [a,b,c]=[1,2,3];
console.log(a,b,c);//1 2 3
let [a,b,c]=[,23,];
console.log(a,b,c);//undefined 23 undefined
//添加默认值
let [a=111,b=2,c=9]=[,,];
console.log(a,b,c);

对象的解构赋值

let {fun,er}={fun:'hi',er:'io'};
//和顺序没有关系
console.log(fun,er);//hi io
console.log(er,fun);//io hi
//对象如果设置了别名,那么原来的名字无效,只是在输出的时候使用别名就行
let {fun:a,bar}={fun:'kl',bar:'123'};
console.log(fun,bar);//fun is not defined
console.log(bar,a);//123 kl
//对象解构赋值写法
let {foo:abc='rgafghg',bar}={bar:'fhstfhjsfg'};
console.log(abc,bar);//rgafghg fhstfhjsfg

math对象解构

let {cos,sin,random}=Math;
console.log(typeof cos);//function
console.log(typeof sin);//function
console.log(typeof random);//function

字符串解构赋值

let [a,b,c,d,e]='hjhdyj';
console.log(a,b,c,d,e);//h j h d y
//解决获取字符串的长度方法(对象类型)
let {length}='hjhdyj';
console.log(length);//6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值