"Front-end" JavaScript functions

Source: Internet
Author: User

1, about function parameter--es6 new characteristic rest

Rest saves the parameters of the function in an array-like way

    function Eledis (... rest) {        for (var i=0;i<rest.length;i++ ) {            alert (rest[i]);        }    }    Eledis (1,2,[3,4,5]);

The Eledis function prints the value of each argument.

For JS, it is allowed to pass in any parameter, even if the number of arguments that are called is more than the formal parameter

2. High-order function

A function that can receive a function as its argument

Commonly used are map, reduce, filter, sort

Map: I understand that it is the operation of each element in an array, such as:

var str = [N/a    ]; var strnew = Str.map (        (x)+=            {return + +x;        }        )    Alert (strnew)

Reduce: Receive two parameters, do cumulative operations, such as:

var str = [N/a    ]; var strnew = str.reduce (        (x, y)={            return x+y;        }        )    alert (strnew)

The above function can complete the accumulation of the array

Filter: Filters out elements in the array that you don't want to keep, depending on whether the return value is True or false determines the element's fate, such as:

var str = [N/a    ]; var strnew = Str.filter (        (x)+=            {return x%2==0;        }        )    Alert (strnew)

The above code can implement preserving even numbers in the array

Sort: Sorts an array of elements, such as:

var str = [71,9,0,12,106.23];     var strnew = Str.sort (        (x, y)= ={            ifreturn 1;             Else if return 0;             Else return -1;        }        )    Alert (strnew)

It's actually sorted according to the custom comparison rule ——— the previous element and the subsequent element comparison return to 1

"Front-end" JavaScript functions

Related Article

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.