猿问

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

我有这个:


const id = speakerRec.id;

const firstName = speakerRec.firstName;

const lastName = speakerRec.lastName;

而且我认为有这样的事情,但记不起来了。


const [id, firstName, lastName] = speakerRec;


慕标琳琳
浏览 125回答 2
2回答

森栏

您需要{}用于解构对象属性: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;
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答