数组解构

比如有一个数组:

let arr = [1,2,3]

我想获取其中的值,只能通过角标。ES6可以这样:

const [x,y,z] = arr;// x,y,z将与arr中的每个位置对应来取值
// 然后打印
console.log(x,y,z);

结果:

ES6_解构表达式

 

对象解构

例如有个person对象:

const person = {
    name:"jack",
    age:21,
    language: ['java','js','css']
}

我们可以这么做:

// 解构表达式获取值
const {name,age,language} = person;
// 打印
console.log(name);
console.log(age);
console.log(language);

结果:

ES6_解构表达式

 

 如过想要用其它变量接收,需要额外指定别名:

ES6_解构表达式

 

 {name:n}:name是person中的属性名,冒号后面的n是解构后要赋值给的变量。

 

 

 

相关文章:

  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
猜你喜欢
  • 2021-12-14
  • 2021-05-22
  • 2021-08-03
  • 2021-06-28
  • 2022-12-23
相关资源
相似解决方案