<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 数组解构
let arr = [1, 2, 3]
// let a = arr[0]
// let b = arr[2]
// let c = arr[3]
const [a, b, c] = arr
console.log(a, b, c)
const person = {
name: "jack",
age:18,
sex:"男"
}
// let name = person.name
// let age = person.age
// let sex = person.sex
// console.log(name, age, sex)
// 解构表达式获取值,将 person 里面每一个属性和左边对应赋值
// const {name, age, sex} = person;
// 等价于下面
// 可以分别打印
// console.log(name);
// console.log(age);
// console.log(sex);
//扩展:如果想要将 name 的值赋值给其他变量,可以如下,nn 是新的变量名
const { name: nn, age, sex} = person;
console.log(nn);
console.log(age);
console.log(sex);
</script>
</body>
</html>
ES6数组解构、对象解构
最新推荐文章于 2022-02-09 14:56:43 发布