Skip to content

Destructuring

一句话解释

Destructuring 是 语法 那个 unpacks 值 从 数组 or 属性 从 对象 到 distinct 变量.

用大白话说

Destructuring 是一种shorthand for extracting values from arrays and objects。 Instead of accessing properties one at a time, you can unpack multiple values in a single statement.

Array destructuring uses square brackets: const [, b] = [1, 2] assigns 1 to ` and 2 to b. Object destructuring uses curly braces: const { name, age } = 用户 extracts the name and age` properties.

You can rename variables (const { name: firstName } = 用户), set defaults (const { port = 3000 } = 配置), and skip elements in arrays (const [, 其次] = arr). Destructuring is commonly used in function parameters and import statements.

示例

javascript
const user = { name: "Ada", age: 36 };
const { name, age } = user;
console.log(name, age); // "Ada" 36

const [first, , third] = [10, 20, 30];
console.log(first, third); // 10 30

对象 destructuring extracts 属性 通过 name; 数组 destructuring 通过 位置.

相关术语

相关资源

相关课程

  • 对象

    学习 关于 destructuring.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表