window.onload = function(){ function t1(){ console.log("t1"); } t1(); function t1(){ console.log("new t1"); } t1(); var t1=function(){ console.log("new new t1"); } t1(); function t1(){ console.log("new new new t1"); } t1(); };
为什么结果是:
new new new t1
new new new t1
new new t1
new new t1
刚开始接触js有点不明白,最后一个为什么是打印new new t1而不是new new new t1???
window.onload = function(){ function t1(){ dwn("t1"); } function t1(){ console.log("new t1"); } function t1(){ console.log("new new new t1"); } t1(); // new new new t1 t1(); // new new new t1 t1=function(){ console.log("new new t1"); } t1(); // new new t1 t1(); // new new t1 };
实际执行顺序应该是这样
继续问下为什么是这个执行顺序呢?不是太明白?谢谢了
@亦可追寻: 变量和函数声明会提升到作用域的顶部
看这里:
https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch4.md
@by.Genesis: 谢谢了。