Will the variables declared by let appear in the active object of the scope chain?

A common closure

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script type="text/javascript">
    function createFunction() {
      var result = new Array;
      // for(var i = 0; i < 10; iPP) {
      //   result[i] = function(num) {
      //     return function() {
      //       return num;            
      //     }
      //   }(i)
      // }
      for(let i = 0; i < 10; iPP) {
        result[i] = function() {
          return i;
        }
      }
      return result;
    }
    var arr = createFunction();
    alert(arr[0]());
    alert(arr[1]());
  </script>
</body>
</html>

if you are declaring I with var, when you execute arr [0] (), access the scope chain up, then iDiver2, all output 2;
but if it is let declaration I, when you execute arr [0] (), is there I in the active object of createFunction, and if so, how is its value determined?

Sep.29,2021

first make clear the scope of var and let
the scope of var is at the same level as the for cycle, while let is only valid in this cycle. Each round restates that each method of arr holds a different I


let is the way ES6 declares variables. ES6 supports block-level scopes. If you don't understand, you can think of a brace as a block, such as

.

this is the result of using node in the terminal!

Menu