参考文档:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax
合并数组:
const first = [1, 2, 3];
const second = [4, 5, 6];
[...first, ...second]; // [1, 2, 3, 4, 5, 6]
合并数组的同时插入元素:
['a', ...first, ...second]; // ["a", 1, 2, 3, 4, 5, 6]
['a', ...first, 'b', ...second, 'c']; // ["a", 1, 2, 3, "b", 4, 5, 6, "c"]
复制数组:
[...first]; // [1, 2, 3]
合并对象:
const first = {name: 'tom'};
const second = {age: 18};
{...first, ...second}; // {name: "tom", age: 18}
合并对象并插入新属性:
{...first, ...second, location: 'china'}; // {name: "tom", age: 18, location: "china"}
复制对象:
{...first}; // {name: "tom"}
网友评论