JavaScript ES new feature block scope

JavaScript ES new feature block scope

Preface:

Before learning about block-level scope, we need to have an understanding of scope. The so-called scope is the range within which a member in the code works.

1. What is block scope?

The so-called block-level scope means that the variable can only be used in the code block or sub-code block where it is declared. There is no block-level scope in versions prior to ECMAScript 2015 ECMAScript 2015 provides the let keyword, which enables block-level scope in JavaScript . The sample code is as follows:

/*
 * Block-level scope can only use the let keyword* The let keyword can not only declare block-level scope, but also can be used in global scope and function scope*/
// Global scope let a = 100; // Global variables (function () {
  // function scope let b = 200; // local variable })()
if (true) {
  // block scope let c = 300; // local variable }
console.log(a); // 100
console.log(b); // throws an exception console.log(c); // throws an exception

2. Why do we need block scope?

ECMAScript 5 only has global scope and function scope, and no block scope. This situation presents some problems:

Local variables may overwrite global variables

var v = 100;
(function(){
  console.log(v); // undefined 
  var v = 200;
})


The variable used for counting in the loop body is leaked as a global variable

// Define a loop body for (var v = 0; v < 10; v++) {
  console.log("This is a for loop"); // This is a for loop * 10
}
console.log(v); // 10

If the variable is not released manually after the loop is completed, its life cycle will survive with the script and occupy memory.

3. With function declaration

ECMAScript5 standard stipulates that functions can only be declared in the global scope and function scope, and cannot be declared in the block scope.

Case 1:

if (true) {
  function f() {}
}


Case 2:

try {
  function f() {}
} catch(e) {
  // ...
}

The above two function declarations are illegal according to ECMAScript5 .

ECMAScript 2015 standard stipulates that declaring a function in a block-level scope is similar to using the var keyword, which means that it cannot be accessed outside the current block-level scope.

{
  function fun() {
    console.log('this is fun');
  }
}
fun(); // this is fun
// The above is equivalent to the following function {
  var fn = function () {
    console.log('this is fn');
  }
}
fn(); // this is fn
// If you use the let keyword, you cannot access it outside the block scope {
  let f = function () {
    console.log('this is f');
  }
}
f(); // Throws an exception description of ReferenceError: f is not defined

This is the end of this article about the new block-level scope feature of JavaScript ES . For more information about the new block-level scope feature of ES, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript Basics: Scope
  • JavaScript Advanced Programming: Variables and Scope
  • Graphical explanation of the underlying principle of JavaScript scope chain
  • JavaScript static scope and dynamic scope explained with examples
  • Javascript scope and closure details
  • JS Difficulties Synchronous and Asynchronous and Scope and Closure and Detailed Explanation of Prototype and Prototype Chain
  • A brief discussion on JavaScript scope

<<:  Introduction to commonly used fonts on the Web (fonts supported by iOS and Android browsers)

>>:  Summary of @ usage in CSS (with examples and explanations)

Recommend

Detailed explanation of the use of custom parameters in MySQL

MySQL variables include system variables and syst...

Details on macrotasks and microtasks in JavaScript

Table of contents 1. What are microtasks? 2. What...

Nginx solves cross-domain issues and embeds third-party pages

Table of contents Preface difficulty Cross-domain...

How to install Nginx in Docker

Install Nginx on Docker Nginx is a high-performan...

Vue advanced usage tutorial dynamic components

Table of contents Basic description AST parsing R...

Tutorial on installing mysql5.7.18 on windows10

This tutorial shares the installation and configu...

5 VueUse libraries that can speed up development (summary)

Table of contents What utilities does VueUse have...

A brief discussion on the design of Tomcat multi-layer container

Table of contents Container Hierarchy The process...

How to use javascript to do simple algorithms

Table of contents 1 Question 2 Methods 3 Experime...

Several methods of implementing two fixed columns and one adaptive column in CSS

This article introduces several methods of implem...

How to limit the input box to only input pure numbers in HTML

Limit input box to only pure numbers 1、onkeyup = ...

Understanding and solutions of 1px line in mobile development

Reasons why the 1px line becomes thicker When wor...

Detailed tutorial on how to install mysql8.0 using Linux yum command

1. Do a good job of cleaning before installation ...

Share the responsive frameworks commonly used by web design masters (summary)

This article introduces and shares the responsive...