Symbol.isConcatSpreadable Controls concat
concat spreads array-like values only when Symbol.isConcatSpreadable is true; flipping the flag keeps real arrays nested or flattens array-likes on demand.
Code
const head = [1, 2];
// 数组默认展开,嵌套只浅一层
console.log(JSON.stringify(head.concat([3, 4])));
// 数组类对象默认整体当作一个元素插入
const arrayLike = { 0: 'a', 1: 'b', length: 2 };
console.log(JSON.stringify(head.concat(arrayLike)));
// 打开 isConcatSpreadable:array-like 被逐项展开
const spread = { ...arrayLike, [Symbol.isConcatSpreadable]: true };
console.log(JSON.stringify(head.concat(spread)));
// 关掉它:真数组也能强制保持嵌套
const sealed = [3, 4];
sealed[Symbol.isConcatSpreadable] = false;
console.log(JSON.stringify(head.concat(sealed)));Output
Captured from a sandboxed run of this snippet in the build pipeline.
[1,2,3,4]
[1,2,{"0":"a","1":"b","length":2}]
[1,2,"a","b"]
[1,2,[3,4]]The flow behind the results
The file’s state is a handful of top-level bindings: head, arrayLike, spread, sealed.
The top-level object literal pins length as plain values — no derivation, no imports behind them.
Points of care
- No package manager takes the stage — every call lands on a built-in API.
Core lines
const spread = { ...arrayLike, [Symbol.isConcatSpreadable]: true };
const arrayLike = { 0: 'a', 1: 'b', length: 2 };
sealed[Symbol.isConcatSpreadable] = false;
Interactive Tools for This Topic
FAQ
- What has to be installed first?
- Nothing — the runtime itself provides everything the code touches. Its state lives in module-level bindings: `head`, `arrayLike`, `spread`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.