(function(){
console.log(1);
setTimeout (function(){console.log(2);},1000);
setTimeout (function(){console.log(3);},0);
console.log(4);
})();
输出:
1
4
undefined
3
2
为什么输出中有未定义的内容?
最佳答案
如果您在浏览器本身的控制台中执行此操作,那么每当您 console.log 时它都会打印 undefined,请参阅此线程:
Chrome/Firefox console.log always appends a line saying undefined
还有无数其他线程解释了为什么会发生这种情况。
如果这不在控制台中,而是在您的 JS 文件本身中,那么您代码中的其他地方正在执行此操作
关于javascript - 为什么以下代码给出 'undefined' 内部输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42360550/