Is the function parameter declaration declared by let?

var x = 1;

function foo(x = x) {
  // ...
}

foo() // ReferenceError: x is not defined
In the code above, the parameter x = x forms a separate scope. What is actually executed is let x = x, and this line of code will report an error "x undefined" because of a temporary dead zone.

above is the course content of teacher Ruan Yifeng http://es6.ruanyifeng.com/-sharpdo.

I would like to ask you a question: if it is a let declaration, why does it not report an error if the variable x is repeatedly declared with var in the body of the function?

once the default value of the parameter is set, the parameter forms a separate scope (context) when the function is declared to initialize. When initialization is complete, the scope disappears. This grammatical behavior does not occur when the parameter default value is not set.

if this is the reason, then why would repeating the declaration with let result in an error? after all, it has a different scope

.

-Separator-

well, thank you very much for your answers. I understand what the temporary dead zone of let is. What I don"t understand is why says that function arguments are declared by let

.
Dec.21,2021

After thinking about it for a long time, I found that I was wrong, so I'll answer it again. To "in the above code, the parameter x = x forms a separate scope. What is actually executed is let x = x, and this line of code will report an error of'x undefined' because of the temporary dead zone.

The declaration in the

function is not let. I realized how problematic the above sentence is

until tmd passed the question sent by the master of the building.
var x = 1;
function foo(x = x) {
  // ...
}
foo() 

the error here is not because let x = x;
because the function has a higher promotion priority! is equivalent to the following

function foo(x = x) {
}
var x;
x = 1;
foo();
//Uncaught ReferenceError: x is not defined

so

function foo(x = 1) {
//x=1var x = 1
let x = 2; // 
var x = 3; // xletvar
}

all that should be explained is enough. I will only answer this question

Why are function arguments declared by let
function foo(x = x) {
}

the xampx here has nothing to do with the let declaration.

  1. variable declaration
    let x or var x is a variable declaration.
  2. assignment expression
    x is an assignment expression that contains the = operator, the left identifier x , and the right identifier y .
  3. assignment mode
    function foo (X in {} is the assignment mode in the parameter in the function declaration .

finally, don't try to interpret the syntax with js . If you want to know more about yourself parsing into ast online, you will know. astexplorer


proposal
mdn
the above two have a cursory look at the situation in the unexplained title
Standard
this is about how the function is executed in the standard. According to reason, there should be an explanation for your questions.

12.15.4


:

  1. var x = 1;x;
  2. function foo(x = x)function foo(let x = x) x foofoolet x = xx xxfooxx x

:
-- :


xxvar x = x;xx1undefined


foo x x


var x = 'outer value'

(function() {
//
console.log(x) // ReferenceError
let x = 'inner value' // x
}())
let/const(Lexical Environment)(LexicalBinding)
x xx


letx=xlet x=x;
context
image.png

Menu