数组解构

var names = ["abc", "cba", undefined, "nba", "mba"]

  1. 基本使用

  var [name1, name2, name3] = names

  1. 顺序问题: 严格的顺序

  var [name1, , name3] = names

  1. 解构出数组

  var [name1, name2, ...newNames] = names

  1. 解构的默认值

    var [name1, name2, name3 = "default"] = names

     

对象解构

var obj = { name: "why", age: 18, height: 1.88 }

  1. 基本使用

    var { name, age, height } = obj

  2. 顺序问题

    var { height, name, age } = obj

  3. 对变量进行重命名

    var { height: wHeight, name: wName, age: wAge } = obj

  4. 默认值

    var { height: wHeight, name: wName, age: wAge, address: wAddress = "中国"} = obj

  5. 对象的剩余内容

    var { name,age,...newObj} = obj

posted on 2022-11-22 09:23  fangpiandang  阅读(38)  评论(0编辑  收藏  举报