In-depth Understanding of ES6 001 [Study Notes]

Block-Level Scope Binding
Previously, `var` variable declarations, regardless of where they were declared, were considered to be declared at the top of their scope. Since functions are first-class citizens, the typical order was `function functionName()`, followed by `var variable`.

Block-Level Declarations
Block-level declarations are used to declare variables that cannot be accessed outside the scope of a specified block. Block-level scope exists in:
- Inside functions
- Within blocks (the region between `{` and `}`)

Temporal Dead Zone
When the JavaScript engine scans code and finds variable declarations, it either hoists them to the top of the scope...

Block-level Scope Binding

Previous variable declarations with var were considered to be declared at the top of the scope, regardless of where they were declared. Since functions are first-class citizens, the order is generally function functionName(), var variable.

Block Declarations

Block declarations are used to declare variables that are not accessible outside the scope of a given block. Block scope exists in:

  • Inside functions
  • Inside blocks (the area between characters and { and })

Temporal Dead Zone

When the JavaScript engine scans code and finds variable declarations, it either hoists them to the top of the scope or places them in the TDZ. Accessing variables in the TDZ will trigger a runtime error. Variables are only removed from the TDZ after their declaration has been executed, after which they can be accessed normally.

if(condition){
  console.log(typeof value) // 引用错误
  let value = "blue";
}
// 以下的value并不在TDZ中
console.log(typeof value); // "undefined"
if(condition){
  let value="blue"
}

Block-level Scope Binding in Loops

When using let, the variable only exists within the for loop. Once the loop ends, the variable cannot be accessed.

for(let i=0;i<10;i++){
  process(items[i]);
}
// i 在这里不可访问,抛出一个错误
console.log(i);

Functions in Loops

var declarations make it exceptionally difficult for developers to create functions within loops, because the variable remains accessible outside the loop.

var funs = [];
for(var i=0;i<10;i++){
  funs.push(function(){
    console.log(i)
  })
}
funcs.forEach(function(func){
  func(); // 输出10次数字10
})

To solve this problem, developers use Immediately Invoked Function Expressions (IIFEs) within loops to force the creation of copies of the counter variable. The IIFE creates a copy for each variable `i` it accepts and stores it as the variable `value`. The value of this variable is the value used by the function created in the corresponding iteration, thus calling each function.

var funs = [];
for(var i=0;i<10;i++){
  funs.push((function(value){
    return function(){
      console.log(i)
    }
  })(i))
}
funcs.forEach(function(func){
  func(); // 输出0,1,2...
})

let Declarations in Loops

let declarations simplify the looping process by mimicking everything described above. Each iteration of the loop creates a new variable and initializes it with the value of the variable of the same name from the previous iteration. This also applies to for-in, for-of, and for-each loops.

var funs = [];
for(let i=0;i<10;i++){
  funs.push(function(){
    console.log(i)
  })
}
funcs.forEach(function(func){
  func(); // 输出0,1,2...
})

It is crucial to understand that the behavior of `let` declarations within loops is specifically defined in the standard and is not necessarily related to `let`'s non-hoisting characteristic. In fact, early `let` implementations did not include this behavior; it was added later.

Using const in Loops

In for(const i=0;i<10;i++), an error will occur because i++ attempts to change the value of the constant `i`. However, in for-in and for-of loops, there is no operation that attempts to change the original value of `i`; instead, a new variable is created. Therefore, the execution will be the same as using a let declaration.

Global Scope Binding

Another difference between let and const and var is their behavior in the global scope. When `var` is used in the global scope, it creates a new global variable as a property of the global object. This means `var` is very likely to unintentionally overwrite an existing global variable. However, using let and const cannot overwrite global variables; they can only shadow them. So,

let RegExp = 'Hello world'
console.log(window.RegExp === RegExp)

主题测试文章,只做测试使用。发布者:Walker,转转请注明出处:https://walker-learn.xyz/archives/4308

(0)
Walker的头像Walker
上一篇 Mar 10, 2026 00:00
下一篇 Mar 8, 2026 15:40

Related Posts

  • In-depth Understanding of ES6 006 [Study Notes]

    Symbol and Symbol properties The 6th primitive data type: Symbol. Private names were originally designed to allow developers to create non-string property names, but general techniques cannot detect the private names of these properties. Creating a Symbol let firstName = Symbol(); let person = {} person[firstName] = "Nicholas"; cons…

    Personal Mar 8, 2025
    1.3K00
  • In-depth Understanding of ES6 003 [Study Notes]

    Function parameter default values, as well as some details about the `arguments` object, how to use expressions as parameters, and the temporal dead zone for parameters. Previously, setting default values always relied on expressions containing the logical OR operator. When the preceding value was false, the latter value would always be returned. However, this became problematic if we passed 0 as an argument, requiring type verification. For example, `function makeRequest(url,timeout,callback){ timeout = t...`

    Personal Mar 8, 2025
    1.3K00
  • Go Engineer System Course 004 [Study Notes]

    Requirements Analysis Backend Management System Product Management Product List Product Categories Brand Management Brand Categories Order Management Order List User Information Management User List User Addresses User Messages Carousel Management E-commerce System Login Page Homepage Product Search Product Category Navigation Carousel Display Recommended Products Display Product Details Page Product Image Display Product Description Product Specification Selection Add to Cart Shopping Cart Product List Quantity Adjustment Delete Product Checkout Function User Center Order Center My...

    Nov 25, 2025
    27300
  • Love sports, challenge limits, embrace nature.

    Passion. In this fast-paced era, we are surrounded by the pressures of work and life, often neglecting our body's needs. However, exercise is not just a way to keep fit; it's a lifestyle that allows us to unleash ourselves, challenge our limits, and dance with nature. Whether it's skiing, rock climbing, surfing, or running, cycling, yoga, every sport allows us to find our inner passion and feel the vibrancy of life. Sport is a self-challenge. Challenging limits is not exclusive to professional athletes; it's a goal that everyone who loves sports can pursue. It can...

    Personal Feb 26, 2025
    1.4K00
  • From 0 to 1: Implementing Micro-frontend Architecture 001 [Study Notes]

    Micro-frontends, JS isolation, CSS isolation, element isolation, lifecycle, preloading, data communication, application navigation, multi-level nesting. Note: This uses Mermaid's flowchart syntax, which is supported by Markdown renderers such as Typora, VitePress, and some Git platforms. Retained: Host application main-vue3; child applications: child-nuxt2-home, child-vue2-job, child-vu...

    Apr 20, 2025
    1.6K00
EN
简体中文 繁體中文 English