Spread and REST 运算符
... 语法 for expanding 集合到 元素 and collecting 元素到 数组.
你将学到
- Spread 语法 for 数组 and 对象 (...arr)
- REST 参数到 collect 函数实参 (...args)
- REST in destructuring 到 capture remaining 项
- 复制数组 and 合并对象 immutably
- difference 之间 spread and REST 通过上下文
概念讲解
Same Syntax, Two Roles
The three dots ... have two distinct meanings depending on where they appear. As spread, they expand an iterable into individual elements. As rest, they gather multiple elements into a single array. The syntax is identical; the context determines the role. This duality is elegant but can confuse beginners — remember: spread *expands*, rest *collects*.
Spread in Arrays
Spread lets you inline the contents of one array inside another. This is the idiomatic way to copy an array ([...arr]), concatenate arrays ([..., ...b]), and insert elements at any position without mutation. Because it creates a new array, it works perfectly with the immutability patterns favored by React and modern state management.
Spread in Objects
Object spread ({ ...obj }) copies enumerable own properties into a new object. It is the standard way to merge objects and override specific fields immutably: { ...defaults, ...overrides }. Later properties win, so the order matters. Object spread only does a shallow copy — nested objects are still shared by reference.
Rest Parameters
In a function signature, ...args collects any extra arguments into a real array. This replaces the old 实参 object, which was array-like but lacked methods like 映射 and 筛选. Rest parameters must always be the last parameter in the list.
Rest in Destructuring
Rest also works in destructuring to capture everything that was not explicitly named. const [首先, ...others] = arr gives you the first element and an array of the rest. const { name, ...REST } = obj separates one field from the rest of the object — a common pattern for omitting a key.
示例代码
// Spread: copy and concatenate arrays immutably
const base = [1, 2, 3];
const copy = [...base]; // [1, 2, 3] — independent copy
const extended = [0, ...base, 4]; // [0, 1, 2, 3, 4]
console.log(copy, extended);
// Spread: merge and override objects
const defaults = { theme: "light", fontSize: 14, lang: "en" };
const userPrefs = { fontSize: 18, lang: "zh" };
const settings = { ...defaults, ...userPrefs };
console.log(settings); // { theme: "light", fontSize: 18, lang: "zh" }
// Rest parameters: collect extra arguments into an array
function sum(multiplier, ...numbers) {
return numbers.map((n) => n * multiplier).reduce((a, b) => a + b, 0);
}
console.log(sum(10, 1, 2, 3)); // 60
// Rest in destructuring: separate one field from the rest
const { theme, ...otherSettings } = settings;
console.log(theme); // "light"
console.log(otherSettings); // { fontSize: 18, lang: "zh" }
// Spread with Math.max (which does not accept an array)
const scores = [85, 92, 78, 95, 88];
console.log("Top score:", Math.max(...scores)); // 95
Shows 数组复制/concat 使用 spread, 对象合并使用 override, REST 参数 in 函数, REST in 对象 destructuring 到 omit 键, and spreading 数组到 Math.最大值.
动手试一试
- JSON Formatter
Inspect 数组 and 对象你 合并 and 复制使用 spread.
- 情况 Converter
Spread 是经常 used 到 transform 集合的 文本字段.
常见错误
错误写法
Expecting 对象 spread 到 deep-复制 nested 对象
正确做法
Spread 仅复制 one level. Nested 对象 and 数组是 still shared 通过引用. Use structuredClone(obj) for 真 deep 复制, or spread 每个 nested level manually.
错误写法
Placing REST 参数之前 other 参数
正确做法
REST 参数必须是 最后参数 in 函数 signature. 函数(...args, 最后) 是语法错误. Reorder 所以 ...args comes 最后.
相关资源
相关速查表
- JavaScript Cheatsheet
Spread and REST 语法 for 数组, 对象, and 函数.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。