The js function uses var and let to declare that the former with the same variable name as the parameter is normal, and the latter reports an error.

once the default value of the parameter is set, the parameter forms a separate context when the function is declared to initialize. When initialization is complete, the scope disappears. This syntax behavior is not an extension of the
function when the parameter default value is not set.
var x = 1;
function foo(x, y = function() { x = 2; }) {
  let x = 3;
  y();
  console.log(x);
}

foo() // Uncaught SyntaxError: Identifier "x" has already been declared

Why is it feasible to declare the same parameter variable with var inside the function, and overwrite the variable with the same name in the parameter; but if you use let or const, you will report an error. I can understand the use of let and const, because the argument is equivalent to declaring with let x, and then declaring it with let will report an error


var exists variable promotion


The

var global declaration is actually window. (the declared variable), and in the function it is the private variable, which is the function scope. Let forms a block-level scope, and global declarations are not mounted on window (let, see other blogs for the difference between let and var). As a matter of fact, this is all declared globally, so it will naturally conflict. Check to see if the following code will report an error

var a

var a ={
  function(){
    let a = 1
  }
}
var b ={
  function(){
    let a = 1
  }
}

I have mentioned this question before. You can take a look at my own question. I answered link description

.
Menu