简体   繁体   English

这是console.log的简写吗?

[英]Is this a shorthand for console.log?

In advanced Javascript by Kyle Simpson he says that this: 在Kyle Simpson的高级Javascript中,他说:

var foo = "bar";

can be evaluated like this: 可以这样评估:

foo;

And that it's just a shortcut for console.log. 而且这只是console.log的快捷方式。

But when I try it nothing shows in the console. 但是当我尝试它时,控制台中什么也没有显示。

Why is that? 这是为什么?

Cheers 干杯

If you type that in the console and press Enter, the console will show you the result of the expression, which is the value of foo . 如果在控制台中键入该内容并按Enter,则控制台将向您显示表达式的结果,即foo的值。 The console shows you the resulting value of any expression you type into it. 控制台将显示您键入的任何表达式的结果值。 In the console itself, there's rarely any need to type console.log . 在控制台本身中,几乎不需要键入console.log (In fact, if you typed console.log(foo); into the console and pressed Enter, you'd see the value of foo followed by undefined , because console.log returns undefined , so the console shows you that value.) (实际上,如果您在控制台中键入console.log(foo);并按Enter,您将看到foo的值,其后是undefined ,因为console.log返回undefined ,因此控制台将向您显示该值。)

In code not typed into the console itself, no, foo; 没有输入到控制台本身的代码中,不, foo; is not shorthand for console.log(foo); 不是console.log(foo);简写console.log(foo); . It's just an ExpressionStatement sitting on its own that basically does nothing. 它只是一个单独的ExpressionStatement ,基本上什么也不做。

foo; is not a shorthand for console.log(foo) . 不是console.log(foo)的简写。

Suppose in code, you have defined a variable var name="mrid" you'll have to write console.log(name) to print it in console. 假设在代码中,您定义了一个变量var name="mrid" ,则必须编写console.log(name)才能在控制台中打印它。 But you can directly type name in console and it will print it's value. 但是您可以直接在控制台中键入name ,它将显示其值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM