Javascript Chrome/Firefox console.log 总是附加一行表示未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14633968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:26:28  来源:igfitidea点击:

Chrome/Firefox console.log always appends a line saying undefined

javascriptdebuggingloggingconsole

提问by N. Chamaa

Every time console.logis executed, a line saying undefinedis appended to the output log.

每次console.log执行时,都会undefined在输出日志中附加一行语句。

Happens in both Firefox and Chrome on Windows and Linux.

在 Windows 和 Linux 上的 Firefox 和 Chrome 中都会发生。

回答by talkol

If you're running console.log()from a JS file, this undefinedline should not be appended.

如果您console.log()从 JS 文件运行,则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, undefinedin this case.

如果您console.log()从控制台本身运行,这是有道理的。这就是为什么:在控制台中,您可以输入变量的名称(例如尝试输入window)并打印有关它的信息。undefined在这种情况下,当您从控制台运行任何 void 函数(如 console.log)时,它还会打印出有关返回值的信息。

I tested both cases on my Chrome (Mac ver 23.0.1271.101) and indeed I see the undefinedline when I run it inside the console. This undefinedalso appears when I write this line in the console: var bla = "sdfdfs"

我在 Chrome(Mac 版本 23.0.1271.101)上测试了这两种情况,undefined当我在控制台中运行它时,确实看到了这一行。undefined当我在控制台中写下这一行时,这也会出现:var bla = "sdfdfs"

回答by Paul Vincent Beigang

Although talkol′s answer is ok, I try to put it more straight:

虽然talkol的回答没问题,但我尽量说得更直:

JavaScript is designed as a dynamic language which means that the type (string, void, boolean …) of a function return value is not pre-defined. If a function does not use a return statement or an empty return statement with no value, JavaScript automatically returns undefined. That means that in JavaScript every function returns something, at least undefined.

JavaScript 被设计为一种动态语言,这意味着函数返回值的类型(字符串、空值、布尔值……)不是预先定义的。如果函数不使用 return 语句或没有值的空 return 语句,JavaScript 会自动返回 undefined。这意味着在 JavaScript 中,每个函数都会返回一些东西,至少是未定义的。

So the function console.log()in Chrome console either uses no or an empty return statement, so that the return value of this function is undefined. This function return value gets also displayed in the Chrome console.

因此console.log()Chrome 控制台中的函数要么使用 no 要么使用空的 return 语句,因此该函数的返回值是未定义的。此函数返回值也会显示在 Chrome 控制台中。

[If somebody know where to find the definition of the console.log()function in Google Chrome source code, please comment with the link, then we can even go further and look at the real code, would be nice.]

【如果有人知道console.log()谷歌浏览器源代码中该函数的定义在哪里可以找到,请评论链接,然后我们甚至可以更进一步查看真正的代码,会很好。】

Sources:

资料来源:

回答by Philippe Piheyns

Follow the picture to solve this problem:

按照图片解决这个问题:

Ctrl + Shift + J

回答by Mahvash Fatima

Console environment in your browser is designed to take the very last statement expression in a program and evaluate it for a value and then show you that value.

浏览器中的控制台环境旨在获取程序中的最后一个语句表达式并评估它的值,然后向您显示该值。

The result of an assignment expression is the value that was assigned. So the JavaScript engine just does an assignment but the console does one extra step which is to set whatever my last statement is, give you that value back. That's why it prints 2:

赋值表达式的结果是被赋值的值。所以 JavaScript 引擎只是做了一个赋值,但控制台做了一个额外的步骤,即设置我最后一条语句是什么,把那个值还给你。这就是它打印 2 的原因:

here.

这里。

In statements that have no return value you get something like undefined.

在没有返回值的语句中,您会得到类似 undefined 的信息。

回答by Sceptic

What you can do is simply create your own console.log like function with a return to change this behavior when doing a lot of coding in the developer console. Here is an example of what that looks like in the developer console:

您可以做的只是创建您自己的类似 console.log 的函数,并在开发人员控制台中进行大量编码时返回更改此行为。以下是开发者控制台中的示例:

console.log('I hate seeing the next line stating the obvious.')
I hate seeing the next line stating the obvious.
undefined
log = function(l){return l}
function log()
if(1 === 2){console.log('1 is not equal to 2.')}else{log('No Shit Sherlock.')}
"No Shit Sherlock."

回答by Yigit Alparslan

undefinedis the return value of the console.log() in chrome dev tools. You will get undefined if you do the following in chrome dev tools, you will see that you get undefined even though x has value 3.

undefined是 chrome 开发工具中 console.log() 的返回值。如果您在 chrome 开发工具中执行以下操作,您将得到 undefined,即使 x 的值为 3,您也会看到您得到 undefined。

> let x = 3
> undefined

回答by i336_

If you're using console.logto emit multiple values in a single line, here's a hacky alternative:

如果您使用console.log在一行中发出多个值,这里有一个 hacky 替代方案:

var1 + ' ' + var2 + ' ' + var...

(Better ideas welcome, this might blow up in certain circumstances)

(欢迎更好的想法,这在某些情况下可能会爆炸)