JavaScript Execution Context Environment

Source: Internet
Author: User

Today, I want to make a deep understanding of JavaScript's execution context. Have always had this plan, but helpless busy doings wherever forget. Read a blog today, the implementation of the context of the understanding can be said to be clairvoyant.

First, the understanding of a piece of code begins

Enter the following code snippet in the browser's console:

This result shows that the code before the execution of a sentence, the browser has done some preparatory work, so, the following two are not error. Let's look at a piece of code below:

The result shows that when the function is declared, the entire function is assigned a value, and the function expression and the variable are just declarations.

Let's summarize what has been done in the prep work:

    • Variable, function expression--variable declaration, the default assignment is undefined;
    • this--assigned value;
    • function declaration--Assignment value;

The readiness of these three kinds of data we call "execution context" or "execution context".

Second, the above is under the global scope of the execution context, the following look at the scope of the function under the context of the environment

The code above shows that the parameters of the arguments variables and functions have been assigned before the statement execution of the function body. As you can see from here, each time a function is called, a new execution context is generated.

Look at the code again:

As you can see, when the function is defined (not when it is called), the scope of the free variable inside the function body is determined.

The final summary:

The context data contents of the global code are:

Common variables (including function expressions),

such as: var a = 10;

Declaration (default assignment is undefined)

function declaration,

such as: function fn () {}

Assign value

This

Assign value

If the code snippet is a function body, you need to attach it on this basis:

Parameters

Assign value

Arguments

Assign value

Value scope of free variables

Assign value

Give the execution context a common definition-- before executing the code, all the variables that will be used are taken out beforehand, some are directly assigned, and some are first empty with undefined.

JavaScript Execution Context Environment

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.