Deep understanding of JavaScript features

Source: Internet
Author: User

recently read the book "JavaScript Advanced Programming ", which mentions the features of JavaScript and is interested in so the combination of their own understanding, here to summarize.

1. JavaScript garbage collection mechanism

5 data types in JavaScript are stored on the stack (Undefined, Null, Boolean, Number, String), non-basic data types are stored in the heap, memory is consumed, and the heap is not automatically freed by the program.

A picture to understand the location of data types in JS: Basic data types are stored in the stack, non-basic data types are stored in the heap (objects), variable obi1 is stored in the stack is only the address of the object in the heap.

Declares a variable a= object, simply declares that a pointer address points to the memory space where the object is stored, and each additional pointer address that points to the same memory space, the object's reference count is added by 1.

If an object is no longer required for the code to run, but the reference count is not zero, the garbage collection mechanism cannot release the memory, which is known as a memory leak.

The way to free memory is to touch a reference to the memory address, set the reference to his pointer address to NULL, such as: A=null;

2. JavaScript single thread (only one thing at a time) core features

JS There is a task queue in addition to the main thread, (JS single thread All tasks must be queued for completion).

JS computing power is weak if there is a lengthy task, the latter task must wait a long time for the previous task to complete, if the network request his time is entirely dependent on the network condition, (Ajax) does not conform to the JS design logic.

The main thread can ignore the IO (input/output) device, suspend the waiting task (the task of listening for event binding, AJAX network request) First run the task in the queue, wait until the IO device has the return result, and then go back to the Suspend task execution (asynchronous event).

All tasks are divided into two kinds of synchronous synchronous events asynchronous asynchronous events, in JS:

Synchronization events are queued for execution on the main thread (the previous execution is complete before the next one can be executed).

When an asynchronous event does not enter the main thread and enters the task queue (task queues), only the Tasks task queue notifies the main thread that an asynchronous task can be executed and the task is removed from the task queue to the main thread.

3. JavaScript running mechanism steps

(1) All synchronization tasks are executed sequentially on the main thread to form (the execution stack). The definition of the stack: Advanced post-out, LIFO.

For example: To define three functions, the SayHello function is called directly in the student function body, and the Isadult function is passed as a parameter to the SayHello function, where Isadult is also a callback function.

The order in which three functions enter the execution stack is: Student,sayhello,isadult

function Student () {varAge = -; varName ='Xiao Li'SayHello (Name,age,isadult (age)) Console.log ("Student Function") //Last Stack } function Isadult (age) {varresult = Age >= -?'Adult':'Minors'Console.log ("Isadult function") //First out stack returnresult; } function SayHello (name,age,reust) {Console.log ('hello! this is'+name+' '+age+'years old me'+reust) Console.log ("SayHello function")        }
        Student ();

Look at the output: Three functions enter the execution stack sequentially: Isadult,student,sayhello

(2) a task queue exists outside the main thread, and the task is placed in the task queue as long as the asynchronous task has returned results.

(3) After all the tasks in the execution stack have been executed, the task queue is accessed, the events in the queue are checked, the first events waiting in the task queue are taken, the event goes to the execution stack, and execution begins.

(4) Repeat the third step continuously.

4. JavaScript callback function callback (event binding function)

Example: Binding a Point-and-click event, JS invokes an event-bound function when the hit event triggers the function called the event's callback function (the asynchronous task must specify a callback function).

Task Queue FIFO data structure, the preceding event is first read by the main thread, so if there is a ' timer ' in the queue, the timer may not be triggered on time, depending on the end of the function execution in the previous task queue.

5. Memory area in JavaScript

When the program runs, the memory space is required to hold the data, respectively, stack (stack) and heap (heap).

Stacks are structured, each chunk in a certain order to take storage, you can clearly know the size of each chunk, and the heap is no structure, the data stored arbitrarily, so the stack calls faster than the heap.

Each thread allocates a stack, each process allocates a heap, the stack is thread exclusive, and the heap is thread-common.

The stack is automatically released by the system, and the heap is manually opened and released by the programmer.

Deep understanding of JavaScript features

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.