这是我的 JavaScript 代码:
console.log(a);
c();
b();
var a = 'Hello World';
var b = function(){
console.log("B is called");
}
function c(){
console.log("C is called");
}
现在这里是输出:
undefined
hoisting.html:12 C is called
hoisting.html:6 Uncaught TypeError: b is not a function
我的问题是为什么 c() 和 b() 的行为不同。 b 应该抛出类似 b is not Defined 的错误。
函数声明将与其主体一起提升。
函数表达式不会,只有 var 语句会被提升。
这就是您的代码在编译后 - 运行时之前对解释器“看起来”的样子:
var c = function c(){
console.log("C is called");
}
var a = undefined
var b = undefined
console.log(a); // undefined at this point
c(); // can be called since it has been hoisted completely
b(); // undefined at this point (error)
a = 'Hello World';
b = function(){
console.log("B is called");
}
KISSJavaScript
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)