var x = 1;
function foo(x, y = function() { x = 2; }) {
var x = 3;
y();
console.log(x);
}
foo() // 3
x // 1
the above is the penultimate example of the scope of the function in the book introduction to es6 written by teacher Ruan Yifeng . Teacher Ruan Yifeng"s explanation is as follows
. In theabove code, the parameters of the function foo form a separate scope. In this scope, you first declare the variable x, and then declare that the default value of the variable yrecoery y is an anonymous function. The variable x inside this anonymous function points to the first parameter x in the same scope. An internal variable x is declared inside the function foo, which is not the same variable as the first parameter x because it is not in the same scope, so after executing y, the values of both the internal variable x and the external global variable x remain unchanged.
the running result in the browser is as expected
Babeles5
function execution is no longer 3
in the es6 environment.according to teacher Ruan Yifeng
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.
so how do you understand the scope of this parameter, and the es6 code changes when it is converted through Babel and put into the browser to run? Does this mean that it is the problem of Babel conversion? So what should be the standard for es6?