The strict mode of parsing sequence in JS

Source: Internet
Author: User

First, the parsing sequence of JavaScript

The code that we all understand is executed from top to bottom, but it's actually not. Let's take a look at the following code.

  

1 alert (a); var a = 1;

If the execution order is from top to bottom, pops up a A, the browser will think from top to bottom execution, then when it is alert (a), he will find that there is no this thing, then he will error, but in fact he bounced out of the result is undefined. The return value is undefined description A is not defined and is not assigned. Let me explain the sequence of JavaScript parsing.

Keywords with declarative meaning in 1.ES5

VAR has variable promotion

function also has the function of declaring variables.

2. Parse Order

1. Find the Declaration var, function declaration: Just declare the variable, not including the assignment.

2. Implementation

Note: The above two steps are followed from top to bottom, and when executed, an equal sign is encountered and the right side of the equal sign is first seen.

Note: When a function declares a variable with the same name as the Var declaration, the function's variable weight is higher than the Var declaration.

Here are a few more examples to resolve a lot of clear, but look at the example before you know what is called scope.

Second, scope

Scopes are: The range of functions is divided into the following two types

1. Global scope

2. Function scope

The difference between the two of them looks at the following example for careful analysis.

Three, see a few examples of the steps to parse the execution sequence

1. A first example:

var x = 5;    A ();     function A () {        alert (x);         var x = ten;    } alert (x);

Parsing process

1. Look for a declaration (see global scope)

var x;

function A () {}

2. Implementation

x = 5;

A ()-------------> perform the above two steps in the process of performing this function

1, look for the declaration var x; (function Scope)

2. Implementation

alert (x); This x exists in the scope of the function, and has not performed to the assigned value of the step, then the thing that pops up is undefined;

x = 10;

Alert (x) Here the popup is the global variable 5;

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

2. A second example

function A () {    alert (x);     var x = ten;} alert (x);

The parsing process is analyzed as in the example above

1. Find a statement

function A () {}

2. Implementation

A ()-------------------------> function

1. Find a statement

var x;

2. Implementation

Alert (x) popup undefined

x = 10;

alert (x); The x here will look for x from the global, but there is no X, so the browser will error that x is not defined X was not defined

So the popup content of the browser is undefined error

 

I believe that the people who have read these two examples have a clear understanding of the process, and if they do not understand it, it is recommended to look again.

Here are a few areas to be aware of, directly on the example

3. A third example

As mentioned earlier, when a function declares a variable with the same name as a var declaration, the function's variable weight is higher than the Var declaration. An example to prove

  

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

Parsing process

1. Find a statement

function A () {}

var A;

2. Implementation

Alert (a) says that the declaration of function is higher than the Var declaration, and all this time he will pop up the function block (function body).

A = 1;

alert (a); This is the 1 pop-up.

So the final result is the function block 1;

4. A fourth example

A child scope can look up variables to the parent scope until the global scope, and vice versa. If the sub-scope has the same variable, then he will use his own, not to look for his father.

  

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

Parsing process

1. Find a statement

var A;

function fn () {}

2. Implementation

A = 5;

fn ()--------------------------------------> functions

1. Find a statement

2. Implementation

alert (a);  He doesn't have a here, so go find dad. A = 5; so the pop-up window is 5.

So the final result is the pop-up window 5

Let's see if Dad's going to find his son.

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

1. Find a statement

function fn () {}

2. Implementation

fn ()----------------------------------------> functions

1. Find a statement

1.var b;

2. Implementation

return b;

alert (b); Let's see what the return value is. B was not defined He said that B was not defined, stating that the parent scope could not go to the self-scoped to find the variable.

5. A fifth example

When a variable is out of the way, regardless of the scope from which it comes to the window, let's look at two examples

  

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

This example should be able to analyze the final result is undefined 0

Let's take a look at the next one and you'll be surprised.

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

Obviously all the same, I'm surprised what return value is not undefined and 0?

But have you found the second line above the declaration of the following no declaration, to parse a wave

1. Finding variables

var A;

function fn () {}

2.fn ()----------------------------> functions

A = 1; That's when it comes to that, the variables that are out of 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.

Four, Strict mode

Strict mode code execution, very strict

Variables are not allowed to be fabricated

Meaning: Standardize code development with fluency, logic

  

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

When we write the next two lines of code will not error and problems, but when we add the first line of code, we write this time will be error. So we're going to improve our ability according to standard standards.

Five, maybe a lot of people do the above example does not feel very enjoyable, below I give a few examples, you can analyze the analysis, I will give the answer at the end.

1. First example//10 error

var a = ten; alert (a); a ()function  A () {    alert (a);}

2. A second example undefined 1 0

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

3. The third example, when the same name is repeated in the same declaration, will overwrite the previous write//2 1 1 3

A ()     var function () {        alert (1)    }    A ();     function A () {        alert (2);    }    A ();     var function () {        alert (3);    }    A ()

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

 

The strict mode of parsing sequence in JS

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.