var a=10;
if(a===10){
console.log(a);
function a (){}
console.log(a);
}
因为,如果条件为真,那么为什么 console.log 的值在 chrome v58 中作为函数出现而在 IE 8 中作为 10 出现?请引用 Chrome 和 IE8 控制台输出的屏幕截图。
Chrome :
IE 8:
最佳答案
按照@impregnable fiend 的建议查找函数提升
。在您的代码中,即使您声明了 a=10;
,Javascript 也会扫描所有代码并提取它找到的所有已定义函数,然后再执行任何其他操作。因此,它将找到函数 function a() {}
并在调用 console.log
之前覆盖 a=10
。
关于javascript - 为什么控制台输出作为函数出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45392844/