ES6 Learning Notes (i) Array extension

Source: Internet
Author: User

extension Operators ...

1. Array extension operators convert an array to a comma-delimited sequence of arguments

Console.log (... [//1,2,3])

[' A ', ' B ',... [1,3]]//a,b,1,3

2. Extended Operations for function calls

function push (A, arr) {

A.push (... arr.);

}

****************************************************************************************

function Add (A, b) {

return a+b;

}

var arr=[1,8];

Add (... arr);

****************************************************************************************

function Add (x,y,z,n,m) {

return x+y+z+n+m;

}

var a = [[+]

Add (1,... a,3);

3. Apply extension operator instead of array

Es5

Math.max.apply (null, [1,3,4,5]);

Es6

Math.max (... [1,2,3,5])

Inserts an array into the tail of an array

1) Array.prototype.push (ARR1,ARR2);

2) arr1 = Arr1.concat (ARR2);

3) Arr1.push (... arr2);

Copying an array

arr1 = Arr.concat ()

arr1 = Arr.splice (0)

ARR1 = [... arr]

Structure Assignment

const [A, ... b] = [1,2,3,4]//a=1 b=[2,3,4]

The extension can only be used in the last

4. Convert Strings to arrays

[... ' This is str ']; ["T", "H", "I", "s", "", "I", "s", "", "s", "T", "R"]

Correctly handle strings that contain uicode codes

' x\ud83d\ude80y '. Length//4
[... ' x\ud83d\ude80y '].length//3

Invert string

Let str = ' x\ud83d\ude80y ';

Str.split ("). Reverse (). Join (')
' Y\ude80\ud83dx '

[... str].reverse (). Join (')
' y\ud83d\ude80x '

5. As long as the class array structure of the iterator interface is implemented, the extension operator can be converted to a true array

[... Nodelist]

ES6 Learning Notes (i) Array extension

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.