Home  >  Article  >  Web Front-end  >  Introduction to unpopular knowledge about javaScript

Introduction to unpopular knowledge about javaScript

零下一度
零下一度Original
2017-07-24 09:56:281260browse

Asynchronous: Now and the future

Blocked program

You can write JavaScript program In a separate js file, this program is composed of multiple blocks, only one of these blocks

is executed now, The rest are picked up for execution, and the most common block unit is a function.

For example:

 function now() {
            return 21;
        }
        function later() {
          answer = answer * 2;
          console.log("Meaning of life:", answer);
        }
        var answer = now();
        setTimeout (later,1000); // Meaning of life: 42
          现在:
            function now() {
                return 21;
            }
            function later() {...}
            setTimeout(later,1000);
            setTimeout(later,1000);
          将来:
            answer = answer * 2;
            console.log("Meaning of life:", answer);

Asynchronous console

Event loop

Parallel threads

Asynchrony is about the time gap between now and the future, while parallelism is about things that can happen at the same time.

The most common tools for parallel computing are processes and threads. Parallelism and threads run independently and may run at the same time.

In different Multiple processes can share the memory of a single process, even on different processors.

Concurrency

Two or more "process"Concurrency occurs when executed simultaneously, regardless of whether the individual operations that make up them are executed in parallel

  Concurrency can be regarded as"Process"Grade parallelism is the opposite of computational-level parallelism.

Non-interactive

Two or more "process"When running their steps concurrently and alternately within the same program / events, if these tasks are not related to each other, they do not necessarily need to interact.

Interaction

The more common situation is that concurrent"process"Need to communicate with each other through DOM interaction.

Collaboration

Take a long-running process and split it into steps or batches to make others concurrent"Process"Have the opportunity to insert your own operations into the event loop queue and run them alternately.

Task

Hangs in a queue after each tick of the event loop queue. In a tick , it may be

The occurrence of an asynchronous action will not cause a complete new event to be added to the new event, but will be added to the current event The tick of the adds an item to the end of the task queue.

The order of statements

The order of statements in the code is not necessarily the same as the order in which the JavaScript engine executes the statements.

                               After the JavaScript engine compiles this code, it may find that it is possible to improve the speed by rearranging the order of these statements.

Callback

continuation(continuation)

Sequential brain

Execution and planning

Nested callbacks and chained callbacks

Trust issues

The above is the detailed content of Introduction to unpopular knowledge about javaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn