<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象解构2:嵌套解构</title>
</head>
<body>
<script>
let person = {
name:'gpl',
age:33,
job:{
title:'free'
}
};
let person2 = {};
({name:person2.name,age:person2.age,job:person2.job} = person);
//对象的引用被赋值,所以两者的改动都会影响到对方
console.log(person2.job.title); //free
person.job.title = 'richman';
console.log(person2.job.title); //richman
person2.job.title = 'god';
console.log(person.job.title); //god
//嵌套解构
let {job:{title}} = person;
console.log(title);
//属性未定义的情况下不能使用嵌套解构
let person3 = {
name:'test'
};
//源对象上属性未定义
// //报错:Cannot read properties of undefined (reading 'bar')
// ({
// foo:{
// bar:person3.name
// }
// }=person);
//目标对象上属性未定义
// //报错:Cannot set properties of undefined (setting 'title')
// ({
// job:{
// title:person3.job.title
// }
// } = person);
</script>
</body>
</html>
标签:name,title,对象,解构,person2,嵌套,job,person From: https://www.cnblogs.com/polin/p/17134567.html