Explanation of `let` and block scoping with for loops

后端 未结 3 1104
旧巷少年郎
旧巷少年郎 2020-11-22 01:42

I understand that let prevents duplicate declarations which is nice.

let x;
let x; // error!

Variables declared with let

3条回答
  •  遇见更好的自我
    2020-11-22 02:11

    Is this just syntactic sugar for ES6?

    No, it's more than syntactic sugar. The gory details are buried in §13.6.3.9 CreatePerIterationEnvironment.

    How is this working?

    If you use that let keyword in the for statement, it will check what names it does bind and then

    • create a new lexical environment with those names for a) the initialiser expression b) each iteration (previosly to evaluating the increment expression)
    • copy the values from all variables with those names from one to the next environment

    Your loop statement for (var i = 0; i < 10; i++) process.nextTick(_ => console.log(i)); desugars to a simple

    // omitting braces when they don't introduce a block
    var i;
    i = 0;
    if (i < 10)
        process.nextTick(_ => console.log(i))
        i++;
        if (i < 10)
            process.nextTick(_ => console.log(i))
            i++;
            …
    

    while for (let i = 0; i < 10; i++) process.nextTick(_ => console.log(i)); does "desugar" to the much more complicated

    // using braces to explicitly denote block scopes,
    // using indentation for control flow
    { let i;
      i = 0;
      __status = {i};
    }
    { let {i} = __status;
      if (i < 10)
          process.nextTick(_ => console.log(i))
          __status = {i};
    }   { let {i} = __status;
          i++;
          if (i < 10)
              process.nextTick(_ => console.log(i))
              __status = {i};
        }   { let {i} = __status;
              i++;
              …
    

提交回复
热议问题