Rest Parameters:
In ES5, when you don ' t know how many paramters would be passed in, you can use arguments:
function () { = 0; for (Let i = 0; i < arguments.length; i++) { + = arguments[i] ; } return = SUM (n/a);
In ES6, you can use Rest params:
function (... numbers) {= 0; for (Let i = 0; i < numbers.length; i++) { + = numbers[i] ; } return result;};
function () { It (function() { function(name, ... numbers) {= 0; Numbers.foreach (function(n) { + = n; }) ; return result; }; = DoWork ("Scott", 1,,2,3); Expect (result). ToBe (6); }) ;
... Spread:
It looks the same as Rest Parameters, Spread uses to Spread an array.
function () { function(x, y , z) {return x+y+z; }; Expect (DoWork (... ). ToBe (6);});
<!DOCTYPE HTML><HTML> <Head> <ScriptData-require= "[Email protected]*"Data-semver= "0.0.0-20140302"src= "Https://traceur-compiler.googlecode.com/git/bin/traceur.js"></Script> <Script>Traceur.options.experimental= true; </Script> <ScriptData-require= "[Email protected]*"Data-semver= "0.0.0-20140302"src= "Https://traceur-compiler.googlecode.com/git/src/bootstrap.js"></Script> <Linkrel= "stylesheet"href= "Style.css" /> <Scriptsrc= "Script.js"></Script> </Head> <Body> <Scripttype= "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