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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3d113-4da01.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3d113-4da01.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?