Understanding of JavaScript Local variables and global variables

Source: Internet
Author: User
Tags script tag

<script type= "Text/javascript" >var a = 1; function hehe () {          window.alert (a);          var a = 2;          Window.alert (a); } hehe ();</script>

  

The execution results are as follows:

First Alert:

A second alert:

This is a surprising result, why is the first pop-up box showing undefined instead of 1?

A variable that is defined directly under the script tag in a page is a global variable that is a variable of the Window object, according to the principle of the JavaScript scope chain, when a variable cannot find the definition of the variable under the current scope, Then the JavaScript engine will go up and down the scope chain until it is found in the global scope.

First of all, this program involves the following three concepts execution environment variable object scope chain JS execution Environment sub-global (Browser is the window execution environment) and function execution environment, variable object is used to save the execution environment variables and methods, and a variable object on the scope chain that is placed on one of the variables forms a chain. The execution of this code should be the first step into the global execution environment to build the variable object A (Save the execution environment of the X and an anonymous method), and then go down to the anonymous method execution environment to build variable object B (Save the execution environment of the X), While the variable object of the current execution environment of JS is always placed at the forefront of the scope chain, where the first alert (x) is executed, the variable object b for the current execution environment is saved with X, and in fact there is, but the alert (x) does not assign a value to x, the result is undefined , if X is not present in the variable object B, then the program will follow the scope chain to see if there is an X in the variable object A.

There are two scopes for JS variables: Global variables and local variables. Variables declared without var and variables declared outside the function are global variables, one of the window properties, and variables declared with var belong to the function, regardless of where the function appears, equivalent to declaring at the beginning of the function. Local variables have higher precedence than global variables of the same name, so local variables hide global variables with the same name. To access the hidden global variables, add window. Prefix.
JS has no block scope, and after the statement block, the variables defined by the statement block are accessible until the function ends. For example: for (var idx =0; idx<2;idx++) {} alert (IDX); The results are displayed as 2.

The previous segment of code is equivalent

var x=1;function () {var x;alert (x); X=1212;alert (x);}

Correct code: first time to This.x

<script type= "Text/javascript" >var a = 1; function hehe () {          window.alert (THIS.A);          var a = 2;          Window.alert (a); } hehe ();</script>

  

Understanding of JavaScript Local variables and global variables

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.