JS run mechanism value reference value Pass

Source: Internet
Author: User

1.js is single-threaded why is it single-threaded? Because JS as a browser scripting language, there will be a lot of interaction with the user, as well as the operation of the DOM, multiple threads will be problematic.

2.js has synchronous tasks, asynchronous tasks (Ajax, user clicks, etc., settimeout)

Asynchronous task execution Mechanism:

A. Synchronization tasks are executed on the main thread to form the execution stack;

B. Outside the main thread, there is a task queue, (although JS is single-threaded, but the browser core is multithreaded, in the browser, different asynchronous operations by different kernel modules are scheduled to execute, asynchronous operations back to add related operations to the task queue, different asynchronous operations are added to the task queue time is also different Onclick--dom binding module processing, when the event is triggered, the callback function is added to the task queue immediately; settimeout-Timer Module---Time to join the task queue Ajax---Network module----The request is returned and added to the task queue)

C. Once all synchronization tasks in the execution stack have been processed, the system will automatically read the task queue, enter the execution stack, start execution, and implement the event loop

For example

for (var i=0;i<3;i++) {   setTimeout(function() {        Console.log (i)    },0)   }

The answer is 3 3 3

Parsing: settimeout asynchronous execution, to wait until the main thread executes, the for operation is the main thread task, i=3; SetTimeout is now 3

Change output to 0,1,2 after transformation

for (var i=0;i<3;i++) {   (function(i) {        setTimeout (  function() {           console.log (i)        },0)    }) (i)}    
var obj={i:0}for (; obj.i<3;obj.i++) {   (function(obj) {        setTimeout (function() {           console.log (obj.i)        },0)    
var obj={i:0}for (; obj.i<3;obj.i++) {   (function (obj.i) {        setTimeout(function() {           console.log (obj.i)        },0)    

The answers were 0,1,2 and 3 3 3-0,1,2.

Know the point of knowledge before you say the second question

JS Basic Type number string Boolean undefined null basic type "by value" access stored in the stack to get the actual value

Reference type Object array functions (owning properties and methods) and can modify reference type objects are stored in "Name + heap Address", the process of assignment is name and heap address, the data in heap memory does not change, so two objects can be linkage.

So the above question is actually examining this piece.

var a = 1; var obj = {    2}; var function  = 3function  Test (x, Y, z) {    = 4;     = 5;     = 6;     return  + obj.b + fn.c);

The answer is: ...

JS run mechanism value reference value Pass

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.