87,994
社区成员
发帖
与我相关
我的任务
分享
test();
if(true) {
function test() {
alert('javascript')
}
}else{
function test(){
alert('node.js')
};
}
function test() {
alert('javascript')
}
function test(){
alert('node.js')
};
test();//会正常的输出node.js , 而前面那个函数被覆盖
if(true) {
}else{
}
if(true) {
function test() {
alert('javascript')
}
}else{
function test(){
alert('node.js')
};
}
test();

if(true) {
function test() {
alert('javascript')
}
}else{
function test(){
alert('node.js')
};
}
test();
我的意思是 预解析函数声明会提前的! 你把if去掉把函数调用写上面也可以正确执行啊。!
if(true) {
function test() {
alert('javascript')
}
}else{
function test(){
alert('node.js')
};
}
test();
我的意思是 预解析函数声明会提前的! 你把if去掉把函数调用写上面也可以正确执行啊。!
[/quote]if里面会进行判断然后创建函数。而非通常直接加载函数,因此在你叫test()函数的时候,线程没走到if判断那里。这就是为什么写下面可以
debugger;
if(true){
debugger
function test() {
alert('javascript')
}
}