rest 参数
ES6 引入 rest 参数,用于获取函数的实参,用来代替 arguments
function data(...args) {
console.log(args)
}
data('沈腾', '玛丽') // Array ["沈腾", "玛丽"]
function data2() {
console.log(arguments)
}
data2('本山大叔', '海燕') // 为数组对象 { "0": "本山大叔", "1": "海燕"}
注意点
// rest 参数必须放在参数最后
function fn(a, b, ...args) {
console.log(a)
console.log(b)
console.log(...args)
}
fn(1, 2, 3, 4, 5, 6)
// 1
// 2
// [3, 4, 5, 6]