The default value of the ES6 parameter is a function, and variables are assigned internally.

var x = 1;
function foo(x, y = function() { x = 2; }) {
  var x = 3;
  y();
  console.log(x);
}

foo() // 3
x // 1

if the function assigned by y is externally defined, why is the last global x 1 ;
why does, foo () print 3 if the function assigned by y is internally defined;

Es6
Mar.16,2021

Hey, your observation is correct. This scope is neither "outside" nor "inside", but a scope sandwiched in the middle to prevent the default parameters from being contaminated by variables inside the function.


you need to take a good look at the default values of function parameters in ES6. Recommend a book: " learn more about ES6 ".

in ES6, the default value of the parameter is equivalent to expanding directly:

The
  

code is interesting, but it may not be used much in the actual project.
secondly, this is a closure.
as mentioned above

Menu