gpt4 book ai didi

javascript - 为什么控制台输出作为函数出现?

转载 作者:行者123 更新时间:2023-11-30 21:18:49 25 4
gpt4 key购买 nike

var a=10;
if(a===10){
console.log(a);
function a (){}
console.log(a);
}

因为,如果条件为真,那么为什么 console.log 的值在 chrome v58 中作为函数出现而在 IE 8 中作为 10 出现?请引用 Chrome 和 IE8 控制台输出的屏幕截图。

Chrome :

enter image description here

IE 8:

enter image description here

最佳答案

按照@impregnable fiend 的建议查找函数提升。在您的代码中,即使您声明了 a=10;,Javascript 也会扫描所有代码并提取它找到的所有已定义函数,然后再执行任何其他操作。因此,它将找到函数 function a() {} 并在调用 console.log 之前覆盖 a=10

关于javascript - 为什么控制台输出作为函数出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45392844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com