ECMAScript 6(ES6) 特性概览和与ES5的比较4-扩展参数处理

1.参数的默认值

简单直观的功能参数默认值
ECMAScript 6

function f (x, y=7, z=42) {
   return x + y + z
}
f(1) === 50

ECMAScript 5

function f (x, y, z) {
   if(y === undefined)
      y = 7;
   if(z === undefined)
      z = 7;
   return x + y + z;
};
f(1) === 50;

闲置参数

将剩余参数聚合为可变函数的单个参数
ECMAScript 6

function f (x, y, ...a) {
    return (x + y) * a.length
}
f(1, 2, "hello", true, 7) === 9

ECMAScript 5

function f(x,y){
   var a = Array.prototype.slice.call(arguments, 2);
   return (x + y) * a.length;
}
f(1, 2, "hello", true, 7) === 9;

3.参数传递

将可迭代集合的元素(如数组或甚至字符串)传递到文字元素和单个函数参数中。
ECMAScript 6

var params = ["hello", true, 7]
var other = [1,2, ...params]//[1, 2, "hello", true, 7]

function f (x,y, ...a) {
    return (x + y) * a.length
}
f(1,2, ...params) === 9

var str = "foo"
var chars = [...str]// ["f","o","o"]

ECMAScript 5

var params = ["hello", true, 7]
var other = [1, 2].concat(params);//[1, 2, "hello", true, 7]

function f(x,y){
   var a = Array.prototype.slice.call(arguments, 2);
   return (x + y)* a.length;
}
f.apply(undefined, [1,2].concat(params)) === 9;

var str = "foo";
var chars = str.split("");// ["f","o","o"]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值