js先会预解析,在栈中:
|test| ⇐ 堆 {函数对象}
|a|undefined
|b|undefined
|c|undefined
,执行到a=10,b=11,c=12:
|test| ⇐ 堆 {函数对象}
|a|10
|b|11
|c|12
,执行到test(1);
c:12(全局栈),
函数栈预解析
|a|undefined
|b|undefined
,执行到:
function test(a)
,函数对象的栈:
|a|1
|b|undefined
,执行到a=1,
,函数对象的栈:
|a|1
|b|undefined
,执行到var b=2;
,函数对象的栈:
|a|1
|b|2
执行到c=3;
全局对象的栈:
|a|10
|b|11
|c|3
,执行结束test(1);
console.log(a,b,c);
相当于
console.log(console.log(a,b,c));//外层console.log()是浏览器加的。console.log()无返回值,所以自己的返回undefined.
所以打印:
10 11 3
undefined
函数内部会开辟一个单独的栈空间用于存储预解析的结果。
{a=1;var b=2;c=3;}不光把这些存到堆中,函数的堆空间中还有arguments,[prototype]等对象。