personAge = person.age;
console.log(personName);
console.log(personAge);
然后,是使用对象解构的:
let person = {
name: 'Matt',
age: 27
};
let { name: personName, age: personAge } = person;
console.log(personName);
console.log(personAge);
name: 'Matt',
age: 27
};
let { name, age } = person;
console.log(name);
console.log(age);
let person = {
name: 'Matt',
age: 27
};
let { name, job } = person;
console.log(name);
console.log(job);
let person = {
name: 'Matt',
age: 27
};
let { name, job='Software engineer' } = person;
console.log(name);
console.log(job);
解构并不要求变量必须在解构表达式中声明。不过,如果是给事先声明的变量赋值,则赋值表达式
必须包含在一对括号中:
let personName, personAge;
let person = {
name: 'Matt',
age: 27
};
({name: personName, age: personAge} = person);
console.log(personName, personAge);
name: 'Matt',
age: 27,
job: {
title: 'Software engineer'
}
};
let { job: { title } } = person;
console.log(title);