在最新的 JavaScript 中,什么是更短的表示法



我有这个:

const id = speakerRec.id;
const firstName = speakerRec.firstName;
const lastName = speakerRec.lastName;

我认为有这样的事情,但不记得了。

const [id, firstName, lastName] = speakerRec;
您需要

使用 {} 来解构对象属性:

const {id, firstName, lastName} = speakerRec;

[]用于数组解构:

const [one, two, three] = [1, 2, 3];

示范:

const speakerRec = {
  id: "mySpeaker",
  firstName: "Jack",
  lastName: "Bashford"
};
const { id, firstName, lastName } = speakerRec;
console.log(id);
console.log(firstName);
console.log(lastName);
const [one, two, three] = [1, 2, 3];
console.log(one);
console.log(two);
console.log(three);

它是一个对象,所以使用对象解构(不是数组解构(:

const { id, firstName, lastName } = speakerRec;

最新更新