Home Web Front-end JS Tutorial js parsing order scope strict mode parsing

js parsing order scope strict mode parsing

Oct 23, 2017 am 09:10 AM
javascript Scope order

1. The parsing order of javascript

We all understand that the execution order of the code is from top to bottom, but in fact it is not like this. Let's take a look at the code below.


1 alert(a);
2 var a = 1;
Copy after login

If the execution order is from top to bottom, and an a pops up on top, the browser will think that it is executed from top to bottom, then when it alerts (a) When, 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. The return value is undefined, indicating that a is not defined, that is, it is not assigned a value. Now let me explain the parsing order of javascript.

 1. The declarative keywords in ES5

 var will have variable promotion

  function and also have the effect of declaring variables.

 2. Parsing order

  1. Find the declaration var, function declaration: only declare variables, not including assignments.

2. Execute

Note: The above two steps are followed from top to bottom. When executing the equal sign, look to the right side of the equal sign first.

Note: When a variable declared by function has the same name as a variable declared by var, the variable weight of function will be higher than that declared by var.

It will be much clearer with a few more examples below, but before looking at the examples, you need to know what scope is.

2. Scope

Scope is: the scope of action is divided into the following two types

 1. Global scope

 2. Function effect Domain

The difference between the two is analyzed carefully in the following example.

3. Let’s look at a few examples to analyze the steps of the execution sequence

1. The first example:


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

Analysis process

1. Find the declaration (see global scope)

var x;

function a(){}

2. Execute

  x = 5; , look for the statement var x; (function scope)

       2. Execute

        alert(x); This Then the thing that pops up is undefined;

  5

2. The second example

<p style="margin-bottom: 7px;">a() function a(){<br/>    alert(x);    <br/>    var x = 10;<br/> }<br/>alert(x);<br/></p>
Copy after login

The parsing process is the same as the above example

1. Find the statement

  function a(){}


 2. Execution

  a()----------------------- -->Function

       1. Find the declaration

       var x;

          2. Execute

                          

##                         

##                                                                  �                  �  −・・ ##      x = 10; ## So the pop-up content of the browser is undefined error

I believe that everyone who has read these two examples has a clear understanding of this parsing process. If you still don’t understand it well, it is recommended to read it again. .

The following introduces a few things that need attention. Let’s go directly to the example

3. The third example

As mentioned earlier, when the variables declared by function and the variables declared by var overlap When named, the variable weight of function will be higher than that declared by var. Let’s take an example to prove it

alert(a)function a() {
    alert("函数")
}var a = 1;  
alert(a)
Copy after login

Parsing process

1. Find the statement

function a(){}

var a;

2. Execute


alert(a) As mentioned earlier, the function declaration has a higher weight than the var declaration, so when this is executed, it will pop up the function block (function body )

 a = 1;

  alert(a);   What pops up here is 1

  So the final result is function block 1; .Fourth example

The child scope can look for variables from the parent scope until it reaches the global scope, but not vice versa. If the child 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()
Copy after login

Parsing process

1. Find the statement

var a;

function fn(){}

 2. Execution

 a = 5;


  fn()----------------------- ---------------> Function

                     alert(a); 他这里没有a 所以去找爸爸要。 a = 5; 所以弹窗是 5

  所以最后结果为 弹窗5

  下面看一下爸爸会不会去找儿子要东西


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

  1.寻找声明

    function fn(){}

  2. 执行

    fn()----------------------------------------> 函数

                      1.寻找声明

                        1.var b;

                      2.执行

                        return b;

  alert(b); //我们看一下返回值是多少 b is not defined 他说b没有被定义,说明父作用域不可以向自作用域去寻找变量。

 5. 第五个例子

  当一个变量无中生有时,不管从哪个作用域出来的,统统归到window下,下面看两个例子


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

这一个例子应该可以自己分析了 最后的结果是 undefined 0

我们再来看一下下面这个你会很吃惊


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

  明明都一样,我吃惊什么 返回值不是还是 undefined 和 0 吗

  但是你有没有发现倒数第二行 上面的声明了 下面的没有声明,来解析一波

  1.寻找变量

    var a;

    function fn(){}

  2.fn()---------------------------->函数

          a = 1; 这个时候就说到了那一点,无中生有的变量,统统归到window下面

  所以下面的执行过程

  alert(a) 这里的弹窗就是 1 了

   a = 0;

 alert(a) 弹出 0

  所以最后的结果是 1 0

四、严格模式

  严格模式下的代码执行时,非常严格

  变量不允许无中生有

  意义:规范代码开发的流畅,逻辑


"use strict"a = 1;
alert(a);
Copy after login

当我们写后面两句代码的时候不会报错和出现问题,但是当我们加上第一句代码的时候,我们在这样写的时候就会报错了。所以我们还是按照规范的标准来,提高自己的能力

五、可能好多人做了上面的例子感觉不太过瘾,下面我再给出几个例子,可以自己去分析分析,我会在最后面给出答案。

1. 第一个例子  // 10 报错


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

2.第二个例子 undefined 1 0


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

3.第三个例子 当同样的声明同样的名字重复时,后面写的会覆盖前面写的 //2 1 1 3


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

The above is the detailed content of js parsing order scope strict mode parsing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Usage of typedef struct in c language Usage of typedef struct in c language May 09, 2024 am 10:15 AM

typedef struct is used in C language to create structure type aliases to simplify the use of structures. It aliases a new data type to an existing structure by specifying the structure alias. Benefits include enhanced readability, code reuse, and type checking. Note: The structure must be defined before using an alias. The alias must be unique in the program and only valid within the scope in which it is declared.

How to solve variable expected in java How to solve variable expected in java May 07, 2024 am 02:48 AM

Variable expected value exceptions in Java can be solved by: initializing variables; using default values; using null values; using checks and assignments; and knowing the scope of local variables.

Advantages and disadvantages of closures in js Advantages and disadvantages of closures in js May 10, 2024 am 04:39 AM

Advantages of JavaScript closures include maintaining variable scope, enabling modular code, deferred execution, and event handling; disadvantages include memory leaks, increased complexity, performance overhead, and scope chain effects.

What does include mean in c++ What does include mean in c++ May 09, 2024 am 01:45 AM

The #include preprocessor directive in C++ inserts the contents of an external source file into the current source file, copying its contents to the corresponding location in the current source file. Mainly used to include header files that contain declarations needed in the code, such as #include <iostream> to include standard input/output functions.

C++ smart pointers: a comprehensive analysis of their life cycle C++ smart pointers: a comprehensive analysis of their life cycle May 09, 2024 am 11:06 AM

Life cycle of C++ smart pointers: Creation: Smart pointers are created when memory is allocated. Ownership transfer: Transfer ownership through a move operation. Release: Memory is released when a smart pointer goes out of scope or is explicitly released. Object destruction: When the pointed object is destroyed, the smart pointer becomes an invalid pointer.

Can the definition and call of functions in C++ be nested? Can the definition and call of functions in C++ be nested? May 06, 2024 pm 06:36 PM

Can. C++ allows nested function definitions and calls. External functions can define built-in functions, and internal functions can be called directly within the scope. Nested functions enhance encapsulation, reusability, and scope control. However, internal functions cannot directly access local variables of external functions, and the return value type must be consistent with the external function declaration. Internal functions cannot be self-recursive.

The difference between let and var in vue The difference between let and var in vue May 08, 2024 pm 04:21 PM

In Vue, there is a difference in scope when declaring variables between let and var: Scope: var has global scope and let has block-level scope. Block-level scope: var does not create a block-level scope, let creates a block-level scope. Redeclaration: var allows redeclaration of variables in the same scope, let does not.

There are several situations where this in js points to There are several situations where this in js points to May 06, 2024 pm 02:03 PM

In JavaScript, the pointing types of this include: 1. Global object; 2. Function call; 3. Constructor call; 4. Event handler; 5. Arrow function (inheriting outer this). Additionally, you can explicitly set what this points to using the bind(), call(), and apply() methods.

See all articles