Some methods for callback functions and arrays

Source: Internet
Author: User
about callback Functions

Pass the callback function callback as a parameter to the asynchronous execution function, and then trigger callback execution when the asynchronous execution function finishes executing.

Defines an asynchronous execution function, a callback function as a parameter function
A (callback) {
    callback ();  
    Console.log (' I am an async execution function ');      
}

Define the callback function
B () {
    setTimeout (' Console.log (' I am a callback function ') ', ' n ');     Mimic time-consuming operation  
}

A (B);

I am the asynchronous execution function
//I am the callback function

The code above illustrates that an asynchronous execution function can execute its own code (asynchronous operation) without waiting for the callback function to finish .

Advantages: Simple, easy to understand and deploy

Cons: not conducive to code reading and maintenance, the various parts are highly coupled, each task can only specify a callback function. Array Method

Array.prototype.map ()
The map () method returns a new array in which each element is the result of a callback function.

var numbers = [1, 4, 9];
var roots = Numbers.map (math.sqrt);     The value of the call
//roots in order is [1, 2, 3], the value of numbers is still [1, 4, 9]

Array.prototype.join ()
The join () method joins all elements of an array (or a class array object) into a string .

Array.join (separator) Separator optional. Specifies the delimiter to use. If this argument is omitted, a comma is used as the delimiter.

var array = [1,2,3,4,5];
var str = array.join ("");
str = "12345", array = [1,2,3,4,5]

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.