JavaScript variable scope and memory issues (II)

Source: Internet
Author: User
Tags getcolor variable scope

The execution environment is a particularly important concept in JS, which means that variables or functions can access other data and define their own behavior. Each execution environment has a variable object corresponding to it, and all variables and functions defined in the execution environment are stored in this variable, and we do not see this variable, but we can see it in the background.
The execution environment for global variables is the outermost execution environment, in a Web browser, the global execution environment is the window object, so all functions and global variables can be used as a property of the Window object. Other execution environments destroy memory after the functions and variables have been executed, and variables and functions are destroyed as well, and global variables are destroyed when the page or browser is closed.
"When the code is executed in an environment, a scope chain of variable objects is created (scope chain). The purpose of a scope chain is to ensure an orderly access to all variables and functions that the execution environment has access to. The front end of the scope chain, which is always the variable object for the environment in which the code is currently executing. If the environment is a function, its active object (activation object) is used as the variable object. The active object initially contains only one variable, the arguments object (this object does not exist in the global environment). The next variable object in the scope chain comes from the containing (external) environment, and the next variable object comes from the next containment environment. In this way, it continues to the global execution environment; The variable object of the global execution environment is always the last object in the scope chain. ”
We can draw a lot of particularly important information, the role of the scope chain is to ensure that the execution of the environment all variables and functions of the orderly access, the order is now the most front-end of the scope chain is the arguement object, and the end is the global execution environment of the Window object. Look at the following example:

var color = "Blue"; function ChangeColor () {   var anothercolor = "Red";   function Swapcolors () {       var tempcolor = Anothercolor;       Anothercolor = color;       color = Tempcolor;       You can access color, Anothercolor, and Tempcolor   }   //Here you can access color and anothercolor, but you cannot access Tempcolor   swapcolors ();} Only Colorchangecolor () can be accessed here;

  




Two. Extending the scope chain
Some statements can be executed at the end of the stack in the scope of a variable object, after the execution of the statement is cleared, so that the scope is extended, the following two statements will extend the scope:
The catch statement for the 1.try-catch statement
2.with statements
Such as:

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 properties and methods of the Location object, and the variable object is added to the front end of the scope chain. A variable QS is defined in the BuildUrl () function. When the variable href is referenced in the WITH statement (actually referencing location.href), it can be found in the variable object of the current execution environment. When referring to the variable QS, the variable that is defined in BuildUrl () is referenced in the variable object in the function environment. As for the inside of the WITH statement, a variable named URL is defined, so the URL becomes part of the function execution environment, so it can be returned as the value of the function.
Three. No block level scope
1. In other languages, curly braces are the scope of variables, such as loop statements, and so on, but JS is not a block-level scope.
2.js when declaring a variable, if there is no Var, the default is the global variable.
The 3.js query identifier, in which the order is queried in the local scope first, and then in the global scope.
Such as:

var color = "Blue"; function GetColor () {   var color = "Red";    return color;} "Red" alert (GetColor ());

  

JavaScript variable scope and memory issues (II)

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.