[ES6] 23. Rest Parameters & Spread Parameters
Rest Parameters:
In ES5, when you don't know how many paramters will be passed in, you can use arguments:
let sum = function(){
let result = 0;
for(let i = 0; i < arguments.length; i++){
result += arguments[i];
}
return result;
} let result = sum(1,2,3);
In ES6, you can use Rest params:
let sum = function(...numbers){ let result = 0;
for(let i = 0; i < numbers.length; i++){
result += numbers[i];
}
return result;
};
describe("rest paramters", function(){ it("is like varargs", function(){ let doWork = function(name, ...numbers){ let result = 0;
numbers.forEach(function(n){
result += n;
}); return result;
}; let result = doWork("Scott", 1,,2,3);
expect(result).toBe(6);
});
});
...Spread:
It looks the same as Rest Parameters, Spread uses to spread an array.
it("should sum up", function(){
let doWork = function(x,y,z){
return x+y+z;
}; expect(doWork(...[1,2,3])).toBe(6);
});
<!DOCTYPE html>
<html>
<head>
<script data-require="traceur@*" data-semver="0.0.0-20140302" src="https://traceur-compiler.googlecode.com/git/bin/traceur.js"></script>
<script>
traceur.options.experimental = true;
</script>
<script data-require="traceur@*" data-semver="0.0.0-20140302" src="https://traceur-compiler.googlecode.com/git/src/bootstrap.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head> <body>
<script type="module">
let a = [4,5,6];
let b = [1,2,3, ...a, 7,8,9];
document.write(b); //1,2,3,4,5,6,7,8,9
</script>
</body>
</html>
[ES6] 23. Rest Parameters & Spread Parameters的更多相关文章
- ADOQuery.Parameters: Property Parameters does not exist
Exception class EReadError with message 'Property Parameters does not exist'. Exception class EReadE ...
- Typescript & React & optional parameters & default parameters
Typescript & React & optional parameters & default parameters Typescript & optional ...
- [ES6] Array -- Destructuring and Rest Parameters && for ..of && Arrat.find()
We can use the destructing and rest parameters at the same time when dealing with Array opration. Ex ...
- 【转】es6的拓展运算符 spread ...
原文:https://blog.csdn.net/qq_30100043/article/details/53391308 The rest parameter syntax allows us to ...
- ...:ES6中扩展运算符(spread)和剩余运算符(rest)详解
1.扩展运算符(spread) demo1:传递数据代替多个字符串的形式 let test= function(a,b,c){ console.log(a); console.log(b); cons ...
- Database Initialization Parameters for Oracle E-Business Suite Release 12 (文档 ID 396009.1)
In This Document Section 1: Common Database Initialization Parameters For All Releases Section 2: Re ...
- Device trees, Overlays and Parameters of Raspberry Pi
Raspberry Pi's latest kernels and firmware, including Raspbian and NOOBS releases, now by default us ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)
一. 1.Spring MVC provides several ways that a client can pass data into a controller’s handler method ...
- Oracle Net Listener Parameters (listener.ora)(转)
12/20 7 Oracle Net Listener Parameters (listener.ora) This chapter provides a complete listing of th ...
随机推荐
- VC 无标题栏对话框移动(在OnLButtonDown里再次发送消息)
操作系统:Windows 7软件环境:Visual C++ 2008 SP1本次目的:实现无框移动 所谓的无标题栏对话框,是基于对话框的工程,对话框属性Border设置为None,对话框如下所示: 为 ...
- .Net中C#的DllImport的用法
大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如 Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功 ...
- Android开发之permission
permission,Android权限系统. 基本上都是在manifest.xml文件中进行操作. 1.申请使用权限 申请使用权限使用标记:<uses-permission /> 比如申 ...
- poj1054The Troublesome Frog
链接 想O(n*n)的DP 怎么想都超内存 看讨论有说hash+DP过的 实现比较繁琐 大部分直接暴力过了 直接枚举每个i j 与他们在一条线上的点 是不是给出的点 注意它必须能跳进和跳出 #inc ...
- hdu2852KiKi's K-Number(区间K值)
http://acm.hdu.edu.cn/showproblem.php?pid=2852 区间K值写错了... #include <iostream> #include<cstd ...
- BZOJ_1026_[SCOI2009]_windy数_(数位dp)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1026 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为wi ...
- c语言输入与输出库函数#include<stdio.h>
last modified: 2010-05-28 输入与输出<stdio.h> 头文件<stdio.h>定义了用于输入和输出的函数.类型和宏.最重要的类型是用于声明文件指针的 ...
- my sql 实现批量操作及注意问题
使用in来实现批量操作是个很不错的想法: /*实现查询*/ select * from goods where goodsid in (102,103); /*实现批理删除*/ delete from ...
- NSArray和NSMutableArray的copy和MutableCopy
NSArray: //main.m #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { ...
- QTP常见问题解决方法(一)
1.对脚本的运行速度进行设置 TOOLS->OPTIONS->RUN->RUN MODE 设置就可以了:一般可以设置为500或者1000值,也就是毫秒: QTP 12.0版本: TO ...