[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="[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>
    <script data-require="[email protected]*" 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>
时间: 2024-08-29 03:31:00

[ES6] 23. Rest Parameters & Spread Parameters的相关文章

ES6扩展/收集运算符--spread/rest(3)

ES6引入了一个新的运算符"...",通常称为spread/rest(展开或收集运算符),取决于在哪如何使用,这里先介绍此运算符的概念和基本使用,更多应用将在函数.数组部分学习 第一种:作为扩展运算符(spread)    场景:使用在数组之前. 作用:将一个数组转为用逗号分隔的参数序列 举例1:数组之前 function foo(x, y, z){ console.log(x, y, z) }foo.appley(null, [1, 2, 3]) //在ES6之前我们这样使用数组作为

【转】es6的拓展运算符 spread ...

原文:https://blog.csdn.net/qq_30100043/article/details/53391308 The rest parameter syntax allows us to represent an indefinite number of arguments as an array. Syntax function f(a, b, ...theArgs) { // ... } ---------------------------------------------

Multiple Type Parameters : Generic Parameters

class Pair<KeyType, ValueType> { // Constructor public Pair(KeyType aKey, ValueType aValue) { key = aKey; value = aValue; } // Get the key for this pair public KeyType getKey() { return key; } // Get the value for this pair public ValueType getValue

Android Camera Parameters 方法出错,求教

============问题描述============ public class PhotographActivity extends BaseActivity implements SeekBar.OnSeekBarChangeListener, OnClickListener, Runnable { private SeekBar zoomSet;// 调整焦距 private ImageView takePic, back, flash;// 按钮 private final int F

Database Initialization Parameters for Oracle E-Business Suite Release 12

In This Document Section 1: Common Database Initialization Parameters For All Releases Section 2: Release-Specific Database Initialization Parameters For Oracle 10g Release 2 Section 3: Release-Specific Database Initialization Parameters For Oracle 1

Oracle Net Listener Parameters (listener.ora)(转)

12/20 7 Oracle Net Listener Parameters (listener.ora) This chapter provides a complete listing of the listener.ora file configuration parameters. This chapter contains these topics: Overview of Oracle Net Listener Configuration File Oracle Net Listen

六个漂亮的 ES6 技巧

通过参数默认值强制要求传参 ES6 指定默认参数在它们被实际使用的时候才会被执行,这个特性让我们可以强制要求传参: /** * Called if a parameter is missing and * the default value is evaluated. */ function mandatory() { throw new Error("Missing parameter"); } function foo(mustBeProvided = mandatory()) {

ES6 Syntax and Feature Overview(待续)

View on GitHub Note: A commonly accepted practice is to use const except in cases of loops and reassignment. However, in this resource I'll be using let in place of var for all ES6 examples. Variable: x Object: obj Array: arr Function: func Parameter

ES6部分总结

1.arrows箭头函数语法: =>支持块级函数体, 同时也支持带返回值的表达式函数体. 与普通函数的this作用域不同, 箭头函数内部与它周围代码共享this作用域 即, this指向同一个东西let obj = { name:"leon", arr:[1,2,3,4], printArr () { this.arr.forEach(f => console.log(this.name + "--->" + f)) }}//===========