<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>使用解构赋值</title>
</head>
<body>
<script>
let arr=[1,2,3,4,5];
let [b1,b2]=[arr[2],arr[3]];
console.log([b1,b2]);//[3,4];
let hxj=["haoxuejie",30];
let [name,age]=hxj;
console.log(name);//haoxuejie
console.log(age);//30
function get(){
return ["haoxuejie",30];
}
let [h_name,h_age] =get();
console.log(h_age);
//解构在对象中应用很多,后期vue.js中会常用
//解构在字符串中的应用
const [...m]="hxj";
console.log(m);// ["h", "x", "j"]
let [c,...b]=["hxj","ydc",2020];
console.log(c);//hxj
console.log(b);// ["ydc", 2020]
//使用点语法向数组中追加元素
let d=["hxj",...b];
console.log(d);//["hxj", "ydc", 2020]
console.log(["hxj",b]);//区别于点语法["hxj", Array(2)]
let [e,f="ddd"]=["hxj"];
console.log([e,f]);//["hxj", "ddd"]
function show(args){
console.log(args);
}
show([1,2,3]);//[1,2,3]
function s([name,year]){
console.log([name,year]);
}
s(["hxj",30]);//["hxj", 30]
</script>
</body>
</html>
网友评论