由一组练习题来讲解函数中的块级作用域
练习题:以下三组代码的输出结果
var x = 1;
function func(x, y = function anonymous1() {x = 2}) {
x = 3;
y();
console.log(x);
}
func(5);
console.log(x);
//================
var x = 1;
function func(x, y = function anonymous1() {x = 2}) {
var x = 3;
y();
console.log(x);
}
func(5);
console.log(x);
//================
var x = 1;
function func(x, y = function anonymous1() {x = 2}) {
var x = 3;
var y = function anonymous2() {x = 4};
y();
console.log(x);
}
func(5);
console.log(x);
此题分析:
/!*
* EC(G)中的变量提升
* var x;
* function func(x,y){...} =>func=AAAFFF000
*!/
var x = 1;
function func(x, y = function anonymous1() {x = 2}) {
/!*
* EC(FUNC)私有上下文
初始化作用域链:<EC(FUNC),EC(G)>