<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</style>
</style>
</head>
<body>
<script>
var obj = {name:"Tom",age:18};
// 解构赋值:创建了一个变量 name,值等于 obj.name
let {name} = obj;
console.log(name);
// 解构赋值:创建了一个变量 age,值等于 obj.age
var {age} = obj;
console.log(age);
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</style>
</style>
</head>
<body>
<script>
var obj = {name:"Petter",age:20,gender:"male"};
// 解构赋值:创建了变量 objName,值为 obj.name、变量 objAge 值为 obj.age、变量 objGender 值为 obj.gender
let {name:objName, age:objAge, gender:objGender} = obj;
console.log(objName);
console.log(objAge);
console.log(objGender);
// 解构赋值:创建了变量 objName1,值为 obj.name、变量 objAge1 值为 obj.age、变量 objGender1 值为 obj.gender
var {name:objName1, age:objAge1, gender:objGender1} = obj;
console.log(objName1);
console.log(objAge1);
console.log(objGender1);
</script>
</body>
</html>
网友评论