作用一:
在函数调用中,将剩余参数合并到一个数组中
function showName(firstName, lastName, ...titles) {
console.log( firstName + ' ' + lastName ); // Julius Caesar
// 剩余的参数被放入 titles 数组中
// titles = ["Consul", "Imperator"]
console.log( titles[0] ); // Consul
console.log( titles[1] ); // Imperator
console.log( titles.length ); // 2
}
showName("Julius", "Caesar", "Consul", "Imperator")
function allName(...name) {
console.log(name) // ["王", "二"]
}
allName('王','二')
作用二:
数组添加
let arr1 = [1,2,3]
let arr2 = [4,5,6]
arr1.push(...arr2)
console.log(arr1) // [1, 2, 3, 4, 5, 6]
作用三:
数组合并
let arr1 = ['a','b','c']
let arr2 = ['d','e','f']
let arr3 = ['h','i','j']
let allArr1 = arr1.concat(arr2,arr3) // concat不会改变原数组
console.log(allArr1) // ["a", "b", "c", "d", "e", "f", "h", "i", "j"]
let allArr2 = [...arr1, ...arr2, ...arr3]
console.log(allArr2 ) // ["a", "b", "c", "d", "e", "f", "h", "i", "j"]
作用四:
将字符串装换为数组
let str = 'hello'
let arr = [...str]
console.log(arr) // ["h", "e", "l", "l", "o"]