What is the difference between es5 and es6 scopes
Difference: There are only two types of scope in es5: global scope and function scope, while there are three types of scope in es6: global scope, function scope and block-level scope, with a new one added Block-level scope. The role of block-level scope: It can solve the problem of outer variables being overwritten due to the promotion of inner scope variables, and prevent variables used for loop counting from leaking into global variables.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer
The scope of es5 and es6 Difference:
There are only two scopes in es5: global scope and function scope
Scope in es6 There are three types: global scope, function scope and block-level scope
In Es5, there are only global scope and function scope
ES5 Use var to declare variables. Variables declared with var may exist in the global scope or in the local scope. The specific situation is as follows
1. Global scope
Three situations of having global scope
a. Variables declared outside the function have global scope
b. Undefined variables with direct assignment automatically Declared as a global variable
c. The properties of the window object have global scope
2. Local scope (function scope)
The scope of variables in the function body
Variables defined within the function can only be accessed within the function
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
|
New block-level scope in Es6
Block-level scope can be simply understood as: the content enclosed in curly brackets {}, it can Contains a scope of its own. Variables in block-level scope are declared by let and const
Why is block-level scope needed?
1. Solve the problem of outer variables being overwritten due to promotion of inner scope variables
1 2 3 4 5 6 7 8 9 |
|
Execution results
The variable i in function fun is declared using var. This involves the issue of variable promotion. The so-called variable promotion means that function declarations and variable declarations are always quietly "promoted" to the top of the method body by the interpreter. So the i here is equivalent to reaching the top of function fun in advance, but the assignment is still performed when i = 6 is running. The above code is actually equivalent to:
1 2 3 4 5 6 7 8 9 10 |
|
When the first i is printed , i is only declared but not assigned (i is assigned a value of 6 in the if statement), so the first printed i is undefined, and the second printed i is 6
1 2 3 4 5 6 7 8 9 |
|
If used let declares the variable i in if, then the curly braces { } where the if statement is located will form a block-level scope, and the variables declared in this scope will be "bound" in this area and will no longer be affected by the outside. (i.e. temporary dead zone), so the first i output when executing the fun function is var i=5 in the global scope, and the i output in the if statement is let i=6## declared in the block-level scope.
#2. Prevent variables used for loop counting from leaking into global variables
1 2 3 4 |
|
1 2 3 4 |
|
Block-level scope features
1. Variables declared by let are only valid in the scope (within the current curly braces), so arbitrary nesting is allowed, at each level They are all separate scopes2. The inner scope can have the same name as the outer scope variable (no scopes are used without interfering with each other)3. let can only exist in the current scope Top levelNote: If there are variables/constants declared by let or const in { } in if statements and for statements, the scope of the { } also belongs to the block scopeExamples about scope
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
javascript video tutorial, web front-end】
The above is the detailed content of What is the difference between es5 and es6 scopes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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 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.

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.

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. 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.

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.

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.
