concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
const arr1 = ['a', 'b', 'c']
const arr2 = ['d', 'e', 'f']
console.log(arr1.concat(arr2)) // ["a", "b", "c", "d", "e", "f"]
除了数组作为参数,concat也接受其他类型的值作为参数,添加到目标数组尾部。
const arr = []
arr.concat(1, '1', true, {}, null, undefined) // [1, '1', true, {…}, null, undefined]
如果数组成员包括对象,concat 方法返回当前数组的一个浅拷贝。所谓“浅拷贝”,指的是新数组拷贝的是对象的引用。