JavaScript 的 解构赋值
等级 4241 次按键
这演示了对象和数组的解构以及rest运算符。
JavaScript 代码
// 创建一个具有三个属性的对象1const person = {2 name: "John",3 age: 30,4 city: "Tokyo"5};6 // 对象解构: 将属性提取到变量中// 变量名必须与属性名匹配7const { name, age } = person;8console.log(`${name} is ${age} years old`);9 // 创建一个数字数组10const numbers = [1, 2, 3, 4, 5];// 数组解构: 按位置提取元素// ...rest将剩余元素收集到数组中11const [first, second, ...rest] = numbers;// first=1, second=2, rest=[3,4,5]12console.log(first, second, rest);运行结果
John is 30 years old 1 2 [ 3, 4, 5 ]
可以用这段代码练习打字。成绩不会计入排行榜。
这段代码中的符号
这段 JavaScript 代码中需要输入的符号及其出现次数。
,× 11;× 6.× 5"× 4{× 4}× 4=× 4:× 3(× 2)× 2