spread operator in javascript

后端 未结 3 661
甜味超标
甜味超标 2021-01-17 03:28

In the below code,

function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction(...args);

Is spread operator(...) unpacki

3条回答
  •  醉话见心
    2021-01-17 04:14

    Yes, as per the page that contains the example you posted:

    The spread operator allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected.

    Running the following through an ES6 transpiler:

    function myFunction(x, y, z) {
      console.log(x,y,z);
    }
    var args = [0, 1, 2];
    myFunction(...args);
    

    produces:

    function myFunction(x, y, z) {
      console.log(x, y, z);
    }
    var args = [0, 1, 2];
    myFunction.apply(undefined, args);
    

    which logs 0 1 2, showing it has expanded the args array.

提交回复
热议问题