微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

es6可变参数-扩展运算符

es5中参数不确定个数的情况下:

//求参数和
function f(){
  var a = Array.prototype.slice.call(arguments);
  var sum = 0;
  a.forEach(function(item){
     sum += item*1;          
  })     
  return sum;  
};
f(1,2,3);//6

es6中可变参数:

function f(...a){
  let sum = 0;
  a.forEach(item =>{
     sum += item*1;
  })    
  return sum;  
}
f(1,2,3);//6

...a 为扩展运算符,这个 a 表示的就是可变参数的列表,为一个数组

合并数组

//es5
var param = ['hello',true,7];
var other = [1,2].concat(param);
console.log(other);//[1, 2, "hello", true, 7]
//es6
var param = ['hello',true,7];
var other = [1,2,...param];
console.log(other);// [1, 2, "hello", true, 7]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐