Destructuring Assignment
Unpack 值从 数组 and 对象到 变量使用 concise, readable 语法.
你将学到
- 数组 destructuring 通过位置
- 对象 destructuring 通过属性 name
- 默认值 and renaming 变量
- Nested destructuring for deep 结构
- Destructuring in 函数参数
概念讲解
Array Destructuring
Array destructuring unpacks values by their position. You write a list of variables in square brackets on the left side of an assignment, and JavaScript maps them to the array elements in order. You can skip elements with empty commas and collect the rest with the ... rest pattern.
Object Destructuring
Object destructuring unpacks values by their property name. You write the property names in curly braces, and variables with the same name receive the values. The order does not matter because matching is by key, not position. This is the most common form of destructuring in modern code, especially when receiving function arguments or API responses.
Defaults and Renaming
If a property is undefined, you can provide a default value with the = operator. You can also rename the local variable with a colon: { propertyName: localName }. Combining renaming and defaults gives you { propertyName: localName = defaultValue }.
Nested Destructuring
Destructuring can reach into nested structures by mirroring the shape of the source. For an object like { 用户: { name } }, you write const { 用户: { name } } = 数据. This is powerful but can hurt readability if taken too many levels deep — flatten or destructure in two steps for deeply nested data.
In Function Parameters
Destructuring in function parameters is a clean way to accept many optional arguments without worrying about their order. Instead of 函数(opts) followed by opts.timeout, you write 函数({ timeout = 3000 }) and get named, defaulted parameters. This pattern is ubiquitous in modern libraries and React components.
示例代码
// Array destructuring by position, with skipping
const [, second, third] = ["a", "b", "c"];
console.log(second, third); // "b" "c"
// Rest pattern collects the remaining elements
const [first, ...rest] = [1, 2, 3, 4];
console.log(first, rest); // 1 [2, 3, 4]
// Object destructuring with renaming and defaults
const user = { name: "Ada", age: 36 };
const { name: fullName, role = "guest" } = user;
console.log(fullName, role); // "Ada" "guest"
// Nested destructuring
const response = {
data: { post: { title: "Hello", tags: ["js", "web"] } },
};
const {
data: {
post: { title, tags },
},
} = response;
console.log(title, tags); // "Hello" ["js", "web"]
// Destructuring in function parameters
function render({ title = "Untitled", items = [] } = {}) {
console.log(`#${title}`, items.length, "items");
}
render({ title: "My List", items: [1, 2] });
render(); // uses all defaults
// Swapping variables without a temp
let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b); // 2 1
Demonstrates 数组 destructuring 使用 skipping and REST, 对象 destructuring 使用 rename and 默认, nested destructuring, 函数参数 destructuring 使用 defaults, and classic 变量 swap.
动手试一试
- JSON Formatter
Inspect 对象 and 数组你 将 destructure.
- JSON 树 Viewer
Browse nested 结构之前编写 nested destructuring.
常见错误
错误写法
Using 数组 destructuring 语法 on 对象 or vice versa
正确做法
数组 use square brackets [, b] = arr and match 通过位置. 对象 use curly braces { , b } = obj and match 通过属性 name. Mixing 它们 silently produces undefined 值.
错误写法
Forgetting 外部 parentheses 当 destructuring already-declared 变量
正确做法
不使用 let/const, leading { looks 如块 语句. Wrap assignment in parentheses: ({ name } = obj);. Prefer declaring 使用 const at 相同时间到 avoid 这个.
相关资源
相关速查表
- JavaScript Cheatsheet
Destructuring 模式 for 数组, 对象, and 函数 params.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。