首页 > 其他分享 >数组,对象解构

数组,对象解构

时间:2022-11-22 09:34:24浏览次数:60  
标签:obj name 对象 age 解构 数组 var height

数组解构

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

标签:obj,name,对象,age,解构,数组,var,height
From: https://www.cnblogs.com/fangpiandang/p/16914122.html

相关文章