数组解构
对象解构
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
//数组解构
let array=[1,2,3];
//赋值给abc,以前的做法
let a=array[0];
let b=array[1];
let c=array[2];
//现在的做法
let [d,e,f]=array;
console.log(a,b,c);
console.log(d,e,f);
//对象结构
const object={
name:'张三',
age:11,
language:['java','js','docker']
}
//以前的用法
const name2=object.name;
const age2=object.age;
const language2=object.language;
//现在的用法
const {name,age,language}=object;
//这种用法变量名必须和对象里的名字相同,或者起个变量
const {name1,age1,language1}=object;
const {name:name3,age:age3,language:language3}=object;
console.log(name2,age2,language2);
console.log(name1,age1,language1);//undefined undefined undefined
console.log(name,age,language);
console.log(name3,age3,language3);
</script>
</body>
</html>
网友评论