美文网首页
ES6:rest运算符

ES6:rest运算符

作者: 开车去环游世界 | 来源:发表于2016-12-22 21:46 被阅读35次

rest运算符也是三个点号,不过其功能与扩展运算符恰好相反,把逗号隔开的值序列组合成一个数组。

//主要用于不定参数,所以ES6开始可以不再使用arguments对象
var bar = function(...args) {
    for (let el of args) {
        console.log(el);
    }
}
bar(1, 2, 3, 4);
//1
//2
//3
//4

bar = function(a, ...args) {
    console.log(a);
    console.log(args);
}
bar(1, 2, 3, 4);
//1
//[ 2, 3, 4 ]

rest运算符配合解构使用:

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

相关文章

网友评论

      本文标题:ES6:rest运算符

      本文链接:https://www.haomeiwen.com/subject/kxvhvttx.html