JavaScript closures and Function variable scopes

Source: Internet
Author: User
Tags variable scope

How browser Event loops work

The browser has an event loop that checks the event queue, handles deferred events, UI events (such as clicks, scrolling, and so on), Ajax callbacks, and callbacks provided to settimeout () and SetInterval (), which are sequentially processed by the event loop.

Therefore, when the settimeout () function is called, the provided callback is queued even if the delay is set to 0

The callback stays in the queue until the specified time runs out, and the engine starts to execute the action (if it does not currently perform another action)

Therefore, even if the settimeout () callback is delayed by 0MS, it will still be queued and will not execute until the other non-deferred statements in the function have been executed.

Talking about the comprehension of Nodejs programming from an interview test problem

Original title Link:

Https://cnodejs.org/topic/55145859e26684ed7ff21bd3

1 varvalue1 = 0, value2 = 0, value3 = 0;2  for(vari = 1; I <= 3; i++) {3     varI2 =i;4(function() {5       vari3 =i;6SetTimeout (function() {7Value1 + =i;8value2 + =I2;9Value3 + =i3;Ten}, 1); One     })(); A } -SetTimeout (function() { -     Console.log (value1, value2, value3); the}, 100);

Depending on how the browser event loop works above, you know:

    • The SetTimeout () function is placed in the event queue and is not run until the other non-deferred statements have finished executing.

Analytical:

Function Variable Scope

Value1 Analysis:

In the anonymous function in the settimeout () function

1 function () {2 value1 + = i; 3 value2 + = i2; 4 Value3 + = i3; 5 }

I used the I variable, but the I variable in the settimeout () function in the anonymous function is not defined, so look out, in the settimeout () function is not defined, and then look out for .... Until the for loop is found, but at this point the for loop has been executed, and the value of I is 4;

So the value of value1 is 4+4+4 = 12;

Value2 Analysis:

Similarly, when the settimeout () function executes, the FOR loop is executed. The value of the variable i2 is the value of the I that last entered the loop. That is: 3,3,3 (because when the value of I is 4, it does not satisfy the condition of entering the loop (I <=3)), then: The value of value2 is 3+3+3 = 9

For the value of Value3, it is 1+2+3 = 6;

VALUE3 Analysis:

1 (function() {2  var i3 = i; 3 setTimeout (function() {4 value1 + = i; 5 value2 + = i2; 6 Value3 + = i3; 7 }, 1); 8}) ();

Using an immediately executed function to create a layer closure, this time I as a variable value assigned to the i3, the function is executed immediately, i3 save the value of each;

JavaScript closures and Function variable scopes

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.