Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

js函数提升问题

最近在看《你不知道的javascript》上卷一书里面,发现一个疑问,代码如下:

foo(); // TypeError: foo is not a function
var a = true;
if (a) {
 function foo() { console.log("a"); }
}
else {
 function foo() { console.log("b"); }
}

为什么这里执行foo()会报错?不应该是下面的两个函数整体提升了以后下面的foo覆盖了上面的foo,然后最终输出字符b吗?

没想明白,谢谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

语句块里声明函数的奇葩问题可以参考美团前端二面,读代码题求解


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...