ES6---Extension operators and rest ' ... ' (three-point operators), in arrays, functions, SET/MAP, etc.

Source: Internet
Author: User
Tags new set

ES6 's new three-point operator, which is represented by three dots, plays an important role in the array, can be combined and decomposed into arrays, can transform data structures such as set, and can simplify the representation of function parameters, and then we uncover the mystery ...

Three-point-rest parameter:

Let fun1=function(... args) {For (let Arg of args) {console.log (arg);}; Console.log (args)};fun1 (' A ',' B ',' C ');A b c,[a,b,c]fun1 (1,2);1 2,[1,2] ... args represents all the parameters, regardless of the number of arguments passed through args can be traversed, and args sets all the parameters of the parameter array let fun2=function(Arr,. args) {console.log (arr); Console.log (args);}; Fun2 (1,2,3);  1, [2,3]fun2 (1); function (arr1,.. ARGS,ARR2) {console.log (args);} //at this time error!!! Remember, three is a rest parameter, and no more arguments can be followed, only as the last character! //////////////////////////[x,... y]=[1, 2,3];console.log (y); //[2,3][x,... y,z]=[1,2,3]; //error, Rest element must is last element in array       
As a rest parameter, the three-point operator can play an important role in functions, deconstruction assignments, and so on. ' As the name ', rest means the rest, it pushes the remaining number of arguments into the array, so it inherits the method of the array. The rest parameter can only appear in the last one, or it will be an error, of course, to expand the identity of the operation is a matter of case.

Three o'clock-extended operator:

Let arr=[1,2,3];console.log (... arr.);1, 2, 3 returns the items in the arrayLet a=[2,3];console.log (1,... A,4);The 1,2,3,4 extension operator can be placed in the middleLet Divs=document.queryselectorall (' Div '); [... divs];Array[300],[] DIVs can be transformed into an array, Console.log (... divs);Div1,div2 .... Traversing divs ItemsLetset=New Set ([1,2,3,3]); [...Set];Returns an array of [all-in-a-Let map= new Map ([[1, ' a '],[2, ' B '],[3, ' C ']); [... Map.keys]; //returns [All-in-all], an array of attributes; [... map.values]; //return [a,b,c],value array //////////// ///[...  ' Wbiokr ']; //["W", "B", "I", "O", "K", "R"] traverse strings, return each character; let Str= ' abc '; [ ' aaa ',... str, ' CCC ']; //[aaa, A, B, C, CCC] extended operator location more capricious         
A three-point extension operator that expands an array or class array object into a series of comma-separated sequence of values, which is like the inverse of a rest parameter.

ES6---Extension operators and rest ' ... ' (three-point operators), in arrays, functions, SET/MAP, etc.

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.