I have the following understanding of temporary dead zones in teacher Ruan Yifeng’s ES6 introductory tutorial recently:

// Some "dead zones" are hidden and hard to spot:
function bar(x = y, y = 2) {
  return [x, y];
}

bar(); // Cannot access 'y' before initialization

/* In the code above, calling 'bar' fails (some implementations may not) because the default value of argument 'x' is equal to another argument 'y', which has not yet been declared and is a 'dead zone'. If the default value of 'y' is' x ', no error will be reported because 'x' has already been declared. * /
Copy the code

If function parameters are known to have temporary dead zones,

Y = let

However, we can use var inside functions to denote:

function bar(x) {
    var x = 3;
    console.log(x);
}
bar(2);  / / 3
Copy the code

In normal cases, if we use a variable named let, we cannot use a variable named var:

let x = 2;
var x = 3;
console.log(x); // 'x' has already been declared
Copy the code

Function () is a block-level scope

If so, it should be similar to the let reputation of the for() loop

However:

1. In the for loop

for (let i = 0; i < 3; i++) {
  let i = 'abc';  // No error is reported here
  console.log(i);
}
// 'abc'
// 'abc'
// 'abc'
Copy the code

2. The function

function bar(x) {
    let x = 3;
    console.log(x);
}
bar(2); // Identifier 'x' has already been declared

Copy the code

So this is not a reasonable understanding

————————————————————————–

In the big guy points out:

In js, when a function is executed, parameters and local variables are stored in an active object, or variable object,

If you define a variable in a local variable with the same name as the parameter, the compiler ignores the definition,

All operations on that variable are reflected in the parameter’s variable

function fn(id) {
    console.log(id); / / 2
    var id = 9;
    return arguments[0]}console.log(fn(2)); / / 9
Copy the code

I understand it as:

  1. When a function does not define a variable with the same name as the parameter, the parameter takes the let name,

  2. However, if a variable with the same name as the parameter is defined inside the function, the let name is ignored and all operations are referred to the variable with the same name as the parameter defined inside the function. This process also includes the parameter assignment process, i.e. Id = 2; It can be interpreted as the following

id = 2;
var id = 9;
return id
Copy the code

The variable promotion of the var keyword does not affect the result, because this part of the output is also ok under the global scope.

This is why the let name id is wrong, because before the let name we had executed id = 2;

—————————————————————————————

Thanks for the big guy’s correction, here with the big guy’s original words to explain:

  1. A parameter is neither a VAR nor a let declaration. It is itself a function parameter declaration, a separate type
  1. In addition, function parentheses are function scopes, not block scopes, which are different from block scopes.

—————————————————————————————

Conclusion:

Based on Ruan’s views, my personal understanding is as follows:

  1. The first code does have a temporary dead zone, but it’s not caused by the let, it’s caused by the default name of the function parameter
  1. Redefining the argument inside the function causes all operations on the parameter to be moved to the argument with the same name (because arguement changes)
  1. The argument assignment operation is still executed immediately after the function is executed, which is a reasonable explanation for why we can use var and not let