Shallow Clone (excluding prototype)
let { ...aClone } = a;Rest Properties
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
x; // 1
y; // 2
z; // { a: 3, b: 4 }Nested Objects
let complex = {
x: { a: 1, b: 2, c: 3 },
y: [4, 5, 6]
};
let {
x: { a: xa, ...xbc },
y: [y0, ...y12]
} = complex;
xa; // 1
xbc; // { b: 2, c: 3 }
y0; // 4
y12; // [5, 6]Extending a Function with Additional Options
function baseFunction({ a, b }) {
// ...
}
function wrapperFunction({ x, y, ...restConfig }) {
// do something with x and y
// pass the rest to the base function
return baseFunction(restConfig);
}Extra properties may be safely added to baseFunction.
function baseFunction({ a, b, x }) {
// ...
}
function wrapperFunction({ x, y, ...restConfig }) {
// x won't be accidentally passed to baseFunction after this refactor
return baseFunction(restConfig);
}If the wrapper function wants to consume the x property but also pass it along, it can remerge it in using the spread operator.
function baseFunction({ a, b, x }) {
// ...
}
function wrapperFunction({ x, y, ...restConfig }) {
// explicitly pass x along
return baseFunction({ ...restConfig, x });
}Caveat: Only Own Properties
function ownX({ ...properties }) {
return properties.x;
}
ownX(Object.create({ x: 1 })); // undefinedlet { x, y, ...z } = a;
// is not equivalent to
let { x, ...n } = a;
let { y, ...z } = n;
// because x and y use the prototype chainRestructure using Object Spread Operator
let assembled = { x: 1, y: 2, a: 3, b: 4 };
let { x, y, ...z } = assembled;
let reassembled = { x, y, ...z };Runtime Error
let { x, y, ...z } = null; // runtime errorStatic Error
let { ...x, y, z } = obj; // syntax errorlet { x, ...y, ...z } = obj; // syntax errorSuccessor-ML
val { x = x, y = y, ... = z } = objhttp://successor-ml.org/index.php?title=Functional_record_extension_and_row_capture
Mentioned on the destructuring assignment wiki in previous discussions:
http://wiki.ecmascript.org/doku.php?id=harmony:destructuring#issues