解构赋值
重要性:5
我们有一个对象
let user = {
name: "John",
years: 30
};
编写解构赋值,读取
name
属性到变量name
中。years
属性到变量age
中。isAdmin
属性到变量isAdmin
中(如果不存在该属性,则为 false)
以下是赋值后值的示例
let user = { name: "John", years: 30 };
// your code to the left side:
// ... = user
alert( name ); // John
alert( age ); // 30
alert( isAdmin ); // false
let user = {
name: "John",
years: 30
};
let {name, years: age, isAdmin = false} = user;
alert( name ); // John
alert( age ); // 30
alert( isAdmin ); // false