問題描述
每次執行 console.log
時,都會在輸出日志中附加一行 undefined
.
Every time console.log
is executed, a line saying undefined
is appended to the output log.
它發生在 Windows 和 Linux 上的 Firefox 和 Chrome 中.
It happens in both Firefox and Chrome on Windows and Linux.
推薦答案
如果你從一個 JS 文件運行 console.log()
,這個 undefined
行應該不附加.
If you're running console.log()
from a JS file, this undefined
line should not be appended.
如果您是從控制臺本身運行 console.log()
,那是有道理的.這就是原因:在控制臺中,您可以鍵入變量的名稱(例如,嘗試鍵入 window
)并打印有關它的信息.當您從控制臺運行任何 void 函數(如 console.log)時,它還會打印出有關返回值的信息,在這種情況下為 undefined
.
If you're running console.log()
from the console itself, it makes sense. This is why: In the console you can type a name of a variable (for example try typing window
) and it prints info about it. When you run any void function (like console.log) from the console, it also prints out info about the return value, undefined
in this case.
我在我的 Chrome(Mac 版本 23.0.1271.101)上測試了這兩種情況,當我在控制臺中運行它時確實看到了 undefined
行.當我在控制臺中寫這行時,也會出現這個 undefined
: var bla = "sdfdfs"
I tested both cases on my Chrome (Mac ver 23.0.1271.101) and indeed I see the undefined
line when I run it inside the console. This undefined
also appears when I write this line in the console: var bla = "sdfdfs"
這篇關于Chrome/Firefox console.log 總是附加一行“未定義"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!