The scope problem of JavaScript

Source: Internet
Author: User

JavaScript scopes are the most difficult concepts to understand in JavaScript. Read the "Advanced programming of JavaScript" and other books although it took a lot of effort to understand, it is still a bit confusing to explain. Just on the internet to see a few face questions can be solved by the scope, but also facilitate the understanding of it.

1.JavaScript is a single threaded

Unlike other languages when they were designed, JavaScript is a single thread (only one thing in a time period) and does not support the concept of concurrency. Why is that? Can it be understood that if a thread is creating a DOM object and then the following thread deletes the DOM object, is that contradictory? Another problem is that node, which is now very hot, is a single thread. It is very important that JavaScript is single-threaded for page parsing, from top to bottom. Fool-style execution.

2.JavaScript Pre-parsing mechanism

Let's take a look at the results of a program:

alert (a);  var  a = 1 ; alert (a); //1  function  a   ()  {alert (2);}  alert   (a) ;  //1  var  a = 3 ; alert (a); //3  function  a   ()  {alert (4);}  alert   (a) ;  //3   

The JavaScript parser can be easily divided into two steps when parsing code (in combination with the code above):
Step one: Walk through the code looking for var variables, function functions, parameters
A. Set all var variables to undefined

    • A = undefined

B. Assigning all function bodies to a function pointer

    • A = function A () {alert (2);}
    • A = function A () {alert (4);}

C. Integration of the above two steps, wherein +-*/% and so has the operation function to assign the value, the latter will overwrite the front, so the above a, B two steps after the execution of the remaining:

    • A = function A () {alert (4);}

Step two: Scan code by line
During a progressive scan, the results calculated in the first step are always viewed as a = function A () {alert (4)} and we know that the operator has an assignment function that modifies the value of the variable, and the function does not have the function of assignment. Executes the first line of code discovery The result of alert(a) the first step is that a = function a(){alert(4);} the result of alert (a) is that the function a(){alert(4);} second line of the scan is found to var a = 1; have = has the assignment function can modify the value of the variable to modify the A variable to var a = 1; scan the third row found now var a = 1; so alert(a) The result is 1, scanning the fourth line function a(){alert(2);} of code discovery is a function so there is no assignment function. The value of the variable cannot be modified. So the fifth line alert(a) of code executes the result is 1. Scan the sixth line var a = 3 ; of code so the variable A is modified to the var a = 3 ; seventh line of code execution result is 3; scan line Eighth code found that the function function a(){alert(4);} cannot modify the value of the variable, so alert(a) the result below is 3, the entire process is analyzed.

(There is no discussion of the so-called syntax, lexical knowledge of these compiler principles)

A simple understanding of the 3.JavaScript scope chain

First read the following written question:

str‘jQuery‘;function fun(){     alert(str);//jQuery     str‘JavaScript‘; } fun(); alert(str);//JavaScript

According to the above two-step method analysis, the first step: First look for the var variable, function, parameters

var str = undefined
Fun = Fun () () {...}

Second step: Progressive scan Code The first row str of variables modified to the var str = ‘jQuery‘; second row number of rows do not have the function, the sixth line is a function call, as long as a function call, continue the first step to look for variables, functions and parameters, found that there is no var variable function and parameters, this time will look for variables in the outer layer of the function, This is the concept of the scope chain, which does not continue to look for variables in the outer layer. A variable is found var str = ‘jQuery‘; so the result is jQuery that the Execute code str = ‘JavaScript‘ operation symbol can be assigned, and it is found that there is a variable of str with the same name var str = ‘JavaScript‘ , so the last line executes the result JavaScript .

The above is the content of today, the content is not much but not very good understanding. Suggest multi-analysis and think more. Combined with practical cases ... Practice the truth!


Related articles
    • JavaScript Development Advanced: Understanding JavaScript scopes and scope chains
    • Scope of JavaScript variables



Very happy to exchange study with everybody
Free reprint, creative license, please article source, from here
(Http://blog.csdn.net/unikylin)

The scope problem of JavaScript

Related Article

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.