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