Talking about the execution environment in JS and the _javascript skill of scope

Source: Internet
Author: User

Recently in the interview was asked the scope of the understanding of the chain, feeling that the answer is not very good, today to say JS in the scope chain.

First of all, the implementation of JS in the environment, the so-called implementation environment (sometimes called the environment) it is the most important JavaScript concept. The execution environment defines other data that a variable or function has access to, determining their respective behavior. Each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in this object.

Understanding the execution environment, now look at what is the scope chain. Each function has its own execution environment, and the scope chain of the variable object is created when the code executes in the execution environment. The scope chain guarantees an orderly access to all variables and functions to the execution environment. The front end of the scope chain is always the variable object of the environment in which the currently executing code is located, and if the environment is a function, then its variable object is the active object of the function. The next variable object in the scope chain comes from the containing (external) environment, and the next variable object comes from the next containing environment. This continues to the global execution environment, remembering that the variable object of the global execution environment is always the last object in the scope.

Take a look at the following example:

var scope= "global";
function foo () {
  console.log (scope);
}  
Foo ();

In this example, the scope chain of function foo () contains two objects, one is its own object, and the other is a variable object in the Global environment. Because we can find scope in this scope chain, we can access it within the function.

Looking at an example:

var color = "Blue";
function ChangeColor () {
  var anotercolor = "Red";
  function Swapcolor () {
    var tempcolor = Anotercolor;
    Anotercolor = color;
    color = Tempcolor;
    Console.log (color);
  }
  Swapcolor ();
}
ChangeColor ();

In this example, there are three execution environments: the global Environment, the ChangeColor () local environment, and the Swapcolor () Local environment. Let's take a look at the scope chain of this example.

The rectangle in the diagram represents a specific execution environment. We can see that the variable Tempcolor can only be accessed in the Swapcolor () environment and cannot be accessed in the local or global environment of ChangeColor (). So we can get a conclusion that the internal environment can access all the external environments through the scope chain, but the external environment cannot access any variables and functions in the internal environment. Each environment can search the scope chain up to query for variables and function names, but no environment could enter another execution environment by searching the scope down.

Scope I also want to say is: JS does not block-level scope

Why say JS does not have block level scope? Let's look at the following code:

if (true) {
 var color = ' Blue '; 
}
alert (color);  "Blue"
 

Gee, why is color destroyed after execution of the IF statement? Haha, if in C, C + + or Java, color is actually destroyed, but in JavaScript, the variable declaration in the IF statement adds the variable to the current execution environment (in this case, the global environment). In particular, keep this difference in mind for a for statement, for example:

for (var i = 0;i< i++) {
dosomething (i);
}
alert (i);    10

Remember: in JavaScript, the variable I created by the for statement will still exist in the execution environment outside the loop even after the for loop execution ends.

The above is the entire content of this article, I hope to help you learn.

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.