[JavaScript] ecmascript 6 spread

Source: Internet
Author: User

Recently, when implementing a class method, the younger brother must pass parameters to ensure that the new keyword is not used to generate strength. So it took a lot of effort,CodeStill not beautiful enough. Finally, the answer is es6's spread.

 

See https://github.com/Neverland/Class/blob/master/script/class.js for details

 

Es6 official explanation of SPRED is:

    • Replace an array or array-like actual parameter value with its elements as positional parameters
    • LetNewCompose withThis-Free FormApply
    • Remove (some day, and in similar future methods) automagic (and half-the-time wrong) array flattening, e.g. InArray. Prototype. Concat 

As follows

1. Replace the actual parameter values such as arrays or arrays with multiple positional parameters.

2. You do not need to consider using apply when using new.

3. Remove automated array flattening

 

Syntax:

... Very simple.

 

Case study:

1. If both new and apply are used, the syntax will be incorrect. // new elements. Apply (document. getelementsbytagname ('div '));

 

 
NewElements(...doc ument. getelementsbytagname ('div '));

 

2. Structure assignment, which can be split into segments

 
VaR [A,... B] = [1, 2, 3, 4];
A // 1;
B // [2, 3, 4]

 

3. Directly disassemble class arrays or array parameters when calling a function

VaR A = [1, 2, 4];

If array a is split into four values when a function is called

 
Add. Apply (this, );

Now

 
Add (... );

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.