Rest parameters and extension operators

Source: Internet
Author: User

Rest parameters and extension operators

1.rest parameters

ES6 introduces the rest parameter (in the form of a ... variable name) to get the extra arguments of the function so that the object is not needed arguments . The variable that is paired with the rest parameter is an array that puts the extra arguments into the array.

function Add (... values) {let  sum = 0;  For (var val of values) {    sum + = val;  }  return sum;} Add (2, 5, 3)//10

The function of the above code add is a summation function that, with the rest parameter, can pass any number of arguments to the function.

2. Extension operators

The extension operator (spread) is a three-point ( ... ). It converts an array or an object to a comma-delimited sequence of arguments.

Log Group console.log (... [1, 2, 3]) 1 2 3console.log (1, ... [2, 3, 4], 5)//1 2 3 4 5[...document.queryselectorall (' div ')]//[<div>, <div>, <div>] on object let { x, Y, ... z} = {x:1, y:2, A:3, B:4};x//1y//2z//{a:3, b:4}

3. Analysis

Rest parameters and extension operators can be understood as inverse

Rest parameters and extension operators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.