解构

作者: uuuuuuw | 来源:发表于2021-03-11 15:43 被阅读0次

结构赋值

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语法对象解构和解构默认值实现了一遍,代码就比较简洁

相关文章

网友评论

      本文标题:解构

      本文链接:https://www.haomeiwen.com/subject/jkfoqltx.html