es6运算符-剩余运算符

剩余运算符
当函数参数个数不确定时,用 rest运算符

function rest1(...arr) {
    for (let item of arr) {
        console.log(item);
    }
}
rest1(1, 2, 3);
// 结果
// 1
// 2
// 3

当函数参数个数不确定时的第二种情况

function rest2(item, ...arr) {
    console.log(item);
    console.log(arr);
}
rest2(1, 2, 3, 4, 5);
// 结果
// 1
// [2, 3, 4, 5]

解构使用

var [a,...temp]=[1, 2, 3, 4];
console.log(a);
console.log(temp);
// 结果
// 1
// [2, 3, 4]

发布了83 篇原创文章 · 获赞 3 · 访问量 1609

猜你喜欢

转载自blog.csdn.net/weixin_45959525/article/details/104331884