Notes on JavaScript-execution environment and scope

Source: Internet
Author: User

The execution environment is an important concept of javascript, and the execution environment defines the variables that have access to other data that determine their individual behavior, each execution environment

has a variable associated with it, all variables and functions defined in the environment are stored in this object, although the code we write cannot access the object, but

The parser is used in the background when processing data.

The global execution environment is the outermost execution environment, depending on the host environment in which the ECMAScript implementation resides, the objects that represent the execution environment are different

In a Web browser, the global execution environment is considered a Window object, so all global variables and functions are created as properties and methods of the Window object

An execution environment after all the code has been executed, the environment is destroyed, and the definitions of all variables and functions stored therein are destroyed.

Each function has its own execution environment, and when the execution flow enters a function, the environment of the function is pushed into a function stack, and after the function executes, the stack

Eject its environment and return control to the previous execution environment.

When the code executes in an environment. Creates a scope chain (scope chain) for a variable object, and the purpose of the scope chain is to ensure that the execution environment has

Access to all variables and functions ordered access, the front end of the scope chain, is always a variable object in the environment where the code is currently executing.

code example:

<script type= "Text/javascript" >    var color = "Blue";    function ChangeColor () {        var anothercolor = "Red";        function Swapcolor () {            var tempcolor = Anothercolor;            Anothercolor = color;            color = Tempcolor;        }        Here can visit Swapcoloer        swapcolor ();    }    Here you can access Chagnecoler    changecolor ();</script>

Running the Watch console

He has a chain-like structure such as

This diagram represents a specific execution environment where the internal environment can access all external environments through the scope chain, which is the object of the upper chain,

But the external environment (the underlying chain) cannot access any of the internal variables and functions, and the relationships between these environments are linear and sequential, with each

The environment can search up the scope chain.

Of course, you need to extend the scope chain later, and some statements can add variables to the front end of the scope chain, and the object will be executed in the code

After being removed, there are generally 2 ways

(1) Try Catch

(2) With statement

Notes on JavaScript-execution environment and scope

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.