2.1.7 Spread Operator & Rest Operator
其餘運算子(Rest Operator)
function sum(…numbers) {
const result = 0
numbers.forEach(function (number) {
result += number
})
return result
}
sum(1) // 1
sum(1, 2, 3, 4, 5) // 15展開運算子(Spread Operator)
const params = [ "hello", true, 7 ]
const other = [ 1, 2, ...params ]
console.log(other)
// [ 1, 2, "hello", true, 7 ]Last updated