spread operator in javascript

后端 未结 3 619
甜味超标
甜味超标 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:19

    ... is known as Spread/Rest operator depending upon how and where it is used.

    What does spread operator do? The spread operator allows an expression to be expanded in places where multiple elements/variables/arguments are expected.

    Before ES6, arguments is an array like object, corresponds to the arguments passed to a function. Here is a quick look at its use:

    (function(a, b, c){
       console.log(arguments);
    })(1, 2, 3);
    Output would be [1,2,3].
    

    In ES6, the same can be written as:

    function getSum(x, y, z){
       console.log(x+y+z);
    }
    

    can call either of two ways:

    getSum(...[10,20,30]);
    

    or

    var argArr = [10,20,30];
    getSume(...argArr);
    

    We have put ... in front of array, so it spread the elements of array into individual values.

提交回复
热议问题