我正在研究 javascript 的提升概念。
var a = 2;
function a(){};
typeof a
// "number"
var a ;
function a(){};
typeof a
// "function"
我的问题是,为什么第一段代码有 typeof number 而不是第二部分代码中可以看到的 funtion?
最佳答案
如果你破解了第一个代码,它看起来像这样。在 js 中,函数声明提升了函数定义。
要显示函数声明提升函数定义,您可以在声明之前调用函数 a()
var a; // a is undefined
a();
function a() {
console.log('function a');
}; // function is also hoisted
a = 2; // a is assigned a value
console.log(typeof a)
关于javascript - 吊装过程中会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55779660/