结构赋值
es5
function informationFn(data) {
let name = data.name
let age = data.age
let sex = data.sex
let phone = data.phone ? data.phone : "暂无"
}
let person = {
name: "张三",
age: 18,
sex: "男",
phone: undefined
}
informationFn(person)
上面的example中,可以看见代码没有问题,但是如果属性过多,代码就会冗余,使用es6的方式来实现上面的功能看看
function informationFn({ name, age, sex, phone = "暂无"}) {
conslog.log(name, age, sex, phone)
}
let person = {
name: "张三",
age: 18,
sex: "男",
phone: undefined
}
informationFn(person)
上面的example中,我们使用了es6语法对象解构和解构默认值实现了一遍,代码就比较简洁
网友评论