JavaScript variable scope and memory problems (2). javascript Variables

Source: Internet
Author: User
Tags getcolor

JavaScript variable scope and memory problems (2). javascript Variables

Execution environment is a particularly important concept in js. It means that variables or functions can access other data and define their own behaviors. Each execution environment has a corresponding variable object. All variables and functions defined in the execution environment are stored in this variable. We cannot see this variable, but we can see it in the background.
The execution environment of global variables is the peripheral execution environment. In a web browser, the global execution environment is a window object. Therefore, all functions and global variables can be used as an attribute of the window object. Other execution environments destroy the memory after the execution of functions and variables, and the variables and functions are also destroyed. The global variables are also destroyed when the page or browser is closed.
"When the code is executed in an environment, a scope chain of the variable object will be created ). The purpose of the scope chain is to ensure orderly access to all variables and functions that are accessible to the execution environment. The front end of the scope chain is always the variable object in the environment where the code is currently executed. If the environment is a function, the activation object is used as the variable object. The activity object contains only one variable at the beginning, namely, the arguments object (this object does not exist in the global environment ). The next variable object in the scope chain comes from the include (external) environment, and the next variable object comes from the next include environment. In this way, the variable objects in the global execution environment are always the last objects in the scope chain. "
We can draw a lot of important information. The function of the scope chain is to ensure orderly access to all variables and functions in the execution environment. This order is reflected in that the front end of the scope chain is the arguement object, the last end is the window object of the global execution environment. Let's look at the example below:

Var color = "blue"; function changeColor () {var anotherColor = "red"; function swapColors () {var tempColor = anotherColor; anotherColor = color; color = tempColor; // here you can access color, anotherColor, and tempColor} // here you can access color and anotherColor, but you cannot access tempColor swapColors () ;}// here you can only access colorchangeColor ();

  




Ii. extend the scope chain
Some statements can temporarily Add a variable object to the front end of the stack in the scope during execution, and clear the variable object after the statement is executed to extend the scope. The following two statements extend the scope:
1. catch Statement of try-catch statement
2. with statement
For example:

function buildUrl() {   var qs = "?debug=true";   with(location){       var url = href + qs;   }   return url;}

  


Here, the with statement receives the location object, so its variable object contains all the attributes and methods of the location object, and this variable object is added to the front-end of the scope chain. The buildUrl () function defines a variable qs. When the variable href is referenced in the with statement (location. href is actually referenced), it can be found in the variable object in the current execution environment. When the qs variable is referenced, it refers to the variable defined in buildUrl (), which is located in the variable object of the function environment. As for the with statement, a variable named url is defined, so url becomes a part of the function execution environment, so it can be returned as a function value.
Iii. No block-level scope
1. In other languages, curly brackets enclose the scope of variables, such as loop statements, but js does not have block-level scopes.
2. If var is not added when js declares a variable, the global variable is used by default.
3. js query identifier. The order is to first query in the local scope, and then query in the global scope.
For example:

var color = "blue";function getColor(){   var color = "red";    return color;}//"red"alert(getColor());

  

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.