Talking about the strict resolution sequence SCOPE Mode of js, talking about js

Source: Internet
Author: User

Talking about the strict resolution sequence SCOPE Mode of js, talking about js

I. parsing order of javascript

The execution sequence of the code we all understand is from top to bottom, but this is not the case. Let's look at the code below.

alert(a);var a = 1;

If the execution order is from top to bottom, a will pop up on it, and the browser will think that the execution is performed from top to bottom. When it is alert (, he will find that there is no such thing, then he will report an error, but in fact the result he pops up is undefined. If the returned value is undefined, a is neither defined nor assigned a value. Next I will explain the parsing sequence of javascript.

1. keywords with declarative meaning in ES5

The var variable will be upgraded.

Function also has the function of declaring variables.

2. Resolution Sequence

1. Find declaration var and function declaration: only declare variables, not assign values.

2. Run

Note: The above two steps follow the top-down and encounter an equal sign during execution. First, check the right side of the equal sign.

Note: When the variables declared by the function and the variables declared by the var statement are duplicated, the variable weight of the function is higher than that declared by the var statement.

The following are several examples to illustrate clearly, but before looking at the example, we need to know what scope is.

II. Scope

The scope is as follows:

1. Global Scope

2. Function Scope

The differences between the two are analyzed in detail in the following example.

Iii. Let's look at several examples to illustrate the steps of execution sequence.

1. Example 1:

var x = 5; a(); function a(){  alert(x);  var x = 10; }alert(x);

Parsing process

1. Search for declaration (see global scope)

var x;function a(){}

2. Run

X = 5;

A () -------------> when the function is executed, repeat the preceding two steps.

1. Find the Declaration var x; (function scope)

2. Run

Alert (x); this x exists in the function scope and has not been executed to assign values. The pop-up is undefined;

X = 10;

Alert (x) the pop-up window here is global variable 5;

So the content of the pop-up window in the browser is undefined 5.

2. Example 2

 a() function a(){  alert(x);  var x = 10; }alert(x);

The parsing process is analyzed as shown in the preceding example.

1. Search for declaration

Function (){}

2. Run

A () -------------------------> Function

1. Search for declaration

Var x;

2. Run

The alert (x) pop-up is undefined.

X = 10;

Alert (x); here, x looks for x from the global, but no x is found. The browser reports the error "x is not defined x" is not defined.

So the pop-up content in the browser is "undefined" and an error is reported.

 

I believe that those who have read these two examples have a clear understanding of the parsing process. If you still don't know much about it, I suggest you review it.

The following describes some important points and examples.

3. Example 3

As mentioned above, when the variables declared by the function and the variables declared by the var statement are duplicated, the variable weight of the function is higher than that declared by the var statement. Here is an example to prove

Alert (a) function a () {alert ("function")} var a = 1; alert ()

Parsing process

1. Search for declaration

Function (){}

Var;

2. Run

Alert (a) previously mentioned that the function declaration has a higher weight than the var declaration. When you execute this statement, the function block (function body) will pop up)

A = 1;

Alert (a); here the pop-up is 1

So the final result is Function Block 1;

4. Example 4

The sub-scope can find variables from the parent scope until the global scope, and vice versa. If the sub-scope has the same variable, then he will use his own and will not ask his father for it.

var a = 5;function fn() { alert(a)}fn()

Parsing process

1. Search for declaration

Var;

Function fn (){}

2. Run

A = 5;

Fn () --------------------------------------> Function

1. Find the Declaration

2. Run

Alert (a); he does not have a here, so he wants to contact his father. A = 5; so the pop-up window is 5

So the final result is pop-up window 5.

Next, let's see if Dad wants something from his son.

function fn(){  var b = 5;   return b;  }fn();alert(b);

1. Search for declaration

Function fn (){}

2. Run

Fn () ----------------------------------------> Function

1. Search for declaration

1. var B;

2. Run

Return B;

Alert (B); // Let's take a look at the returned value. B is not defined. He said B is not defined, indicating that the parent scope cannot look for variables from the self-scope.

5. Example 5

When a variable is out of nothing, no matter which scope it is out of, it is all under the window. The following two examples are given:

fn(); alert(a); var a = 0; alert(a); function fn(){  var a = 1; }

In this example, the final result is undefined 0.

Let's take a look at this. You will be surprised.

 fn() alert(a) var a = 0; alert(a); function fn(){   a = 1; } 

I'm surprised that the returned values are not undefined or 0.

But have you found that the statement above the last line does not include the following statement to resolve a wave

1. Search for variables

Var;

Function fn (){}

2. fn () ----------------------------> Function

A = 1; at this point, all the variables that come out of nothing are under the window.

So the following execution process

Alert (a) the pop-up window here is 1.

A = 0;

Alert (a) pops up 0

So the final result is 1 0.

Iv. Strict Mode

Strict code execution in strict Mode

Variables cannot be born out of nothing.

Meaning: standardizes the smoothness and logic of code development

"use strict"a = 1;alert(a);

When we write the following two sentences of code, there will be no errors or problems, but when we add the first sentence of code, we will report errors in this way. Therefore, we still follow the standard to improve our capabilities.

5. Many people may not feel quite comfortable using the above example. I will give a few more examples to analyze and analyze them by myself. I will give the answer at the end.

1. The first example // 10 returns an error

var a = 10;alert(a);a()function a(){ alert(20);}

2. The second example undefined 1 0

var a = 0; function fn(){  alert(a);  var a = 1;  alert(a); } fn(); alert(a);

3. In the third example, when the same declaration has the same name, the subsequent statement will overwrite the previously written // 2 1 3

a() var a = function(){  alert(1) } a(); function a(){  alert(2); } a(); var a = function(){  alert(3); } a()

If you see my article to gain some knowledge, then I will be very happy.

The above discussion about the strict scope of the parsing sequence of js is all the content shared by xiaobian. I hope to give you a reference, and I hope you can also support the help house.

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.