简体   繁体   English

控制console.log以获取不返回的功能

[英]Control of console.log for functions without return

I am wondering how to make direct calls to a function that does not have a return and call it via console.log. 我想知道如何直接调用没有返回值的函数并通过console.log调用它。

I learned that functions without return have different control over console.log. 我了解到,不返回的函数对console.log的控制不同。

But I do not know what this means. 但是我不知道这意味着什么。

I have written the example code below and wonder about the output value and undefined. 我在下面编写了示例代码,想知道输出值和未定义。

test code 测试代码

> var bark = function() { return 1; };
undefined
> bark();
1
> console.log(bark());
1
undefined
> var bark2 = function() { console.log('a'); };
undefined
> bark2()
a
undefined
> console.log(bark2());
a
undefined
undefined
>

I'm pretty sure the confusion here is an artifact of the developer console, a combination of the following factors: 我敢肯定,这里的困惑是开发人员控制台的产物,它综合了以下因素:

  • the console displays the return value from every function in the same place that it displays the output of console.log() 控制台在显示console.log()的输出的同一位置显示每个函数的返回值
  • when a function has no return value, it returns undefined 当一个函数没有返回值时,它将返回undefined
  • console.log is a function that has no return value console.log是没有返回值的函数

So the simple statement console.log(1) ends up displaying this in the console: 因此,简单的语句console.log(1)最终在控制台中显示了此语句:

> 1
> undefined

The "1" is the output of the console.log statement. “ 1”是console.log语句的输出。 The "undefined" is the return value of the console.log() function. “ undefined”是console.log()函数的返回值。 (Some browsers display them in a slightly different color or with tiny icons to help you differentiate between output and return values, but it's subtle and easy to confuse the two.) (某些浏览器以略微不同的颜色或带有微小图标的形式显示它们,以帮助您区分输出值和返回值,但将二者混淆起来很细微且容易。)

So to go through your sample output: 因此,通过示例输出:

> var bark = function() { return 1; };
undefined    // <-- return value from defining the function
> bark();    
1            // <-- return value from bark()
> console.log(bark());
1            // <-- return value from bark()
undefined    // <-- return value from console.log()
> var bark2 = function() { console.log('a'); };
undefined    // <-- return value from defining bark2
> bark2()
a            // <-- output of console.log
undefined    // <-- return value of bark2()
> console.log(bark2());
a            // <-- output of console.log inside bark2
undefined    // <-- return value of bark2
undefined    // <-- return value of the console.log wrapping bark2
var bark = function() { return 1; };
bark();

The above function is returning 1. console.log and see: 上面的函数返回1. console.log并查看:

 var bark = function() { return 1; }; console.log(bark()); 

var bark2 = function() { console.log('a'); };
bark2();
console.log(bark2());

When the function is called, it prints a via its own console.log but when the function call is printed we get undefined because console.log will print the value returned from the function which is undefined because the function does not contain any return statement. function被调用时,它打印a通过自己console.log但是当function callprinted我们得到undefined ,因为console.logprint的值returnedfunctionundefined ,因为function不包含任何return声明。

Take a look at the below answer: 看下面的答案:

If you're running console.log() from a JS file, this undefined line should not be appended. 如果您是从JS文件运行console.log(),则不应添加此未定义的行。

If you're running console.log() from the console itself, it makes sense. 如果您从控制台本身运行console.log(),那么这很有意义。 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. 这是为什么:在控制台中,您可以键入变量的名称(例如,尝试键入window),并打印有关该变量的信息。 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. 当您从控制台运行任何void函数(例如console.log)时,它还会打印出有关返回值的信息,这种情况下是未定义的。

- https://stackoverflow.com/a/14634066/10984479 -https://stackoverflow.com/a/14634066/10984479

Void function meaning a function that doesn't return anything. 无效函数表示不返回任何内容的函数。

For easier debugging your console shows the output of all functions/expressions. 为了便于调试,控制台会显示所有函数/表达式的输出。 console.log() returns undefined, so it shows undefined. console.log()返回undefined,因此显示为undefined。

Here you run the bark function, which returns 1, so it outputs 1: 在这里,您将运行bark函数,该函数返回1,因此输出1:

> bark();
1

Here you run the bark function, which returns 1, so it outputs 1. Console.log doesn't return anything, so its logs undefined: 在这里,您将运行bark函数,该函数返回1,因此其输出为1。Console.log不返回任何内容,因此其日志未定义:

> console.log(bark());
1
undefined

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

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