简体   繁体   English

为什么 JavaScript 中的这个 return 语句什么都不返回?

[英]Why does this return statement in JavaScript return nothing?

Before you mark this question as a duplicate please understand that I'm new to JS and always feared asking a question of stackoverflow.在您将此问题标记为重复之前,请理解我是 JS 新手并且总是害怕问一个关于 stackoverflow 的问题。

I dont understand why calling this function returns nothing unless I enclose the function call in a console.log.我不明白为什么调用这个函数什么都不返回,除非我将函数调用包含在 console.log 中。

If I enclose the function call in a console.log I get the expected output "There are 3 elements in this array", however without the console.log, I dont get anything.如果我将函数调用包含在 console.log 中,我会得到预期的输出“这个数组中有 3 个元素”,但是如果没有 console.log,我什么也得不到。

var counter = function (arr) {
    return 'There are ' + arr.length + ' elements in this array';
};

counter(["shaun", "sara", "jessica"])

What I want to know is how I can get the output of this function without using console,.log and the reason why it does not output anything without the console.log.我想知道的是如何在不使用console,.log的情况下获得这个函数的输出,以及为什么没有console.log它不输出任何东西的原因。

console.log() is a function used to print information to the console. console.log()是一个用于将信息打印到控制台的函数。 return on the other hand is a call to pass some value back up to where the call was made.另一方面, return是将某些值传递回调用位置的调用。

Via - CodeCademy Forum通过 - CodeCademy 论坛


return terminates a function and possibly returns a value to the caller of that function (whereas) console.log() will not influence the flow of your code. return终止一个函数并可能向该函数的调用者返回一个值(而console.log()不会影响您的代码流。

Via - Difference between console.log and return in javascript?通过 - 在 javascript 中 console.log 和 return 之间的区别?


Check and run the following Code Snippet for a practical example of how the two works:检查并运行以下代码片段以获取有关两者如何工作的实际示例:

 var x = document.getElementById("first"); var y = document.getElementById("last"); var z = document.getElementById("output"); function printName(){ z.innerText = fullName(); } function fullName(){ console.log(x.value + " " + y.value); // this wont push the concatenated name to printName() return x.value + " " + y.value; // this will push the concatenated name to printName() alert("yu do dis?"); // this won't run anymore since the return statement above prevents the function from invoking anything else } document.getElementById("btn").addEventListener("click", printName)
 <input type="text" id ="first" /> <input type="text" id ="last" /> <button id="btn">Click Me</button> <br/> <div id="full">Hello <span id="output"></span>!!</div>


If the return statement above is removed, the console.log() alone won't return anything to printName() except display the concatenated name in your console.如果上面的return语句被删除, console.log()本身不会向printName()返回任何内容,除了在控制台中显示连接的名称。

var counter = function (arr) {
        return 'There are ' + arr.length + ' elements in this array';
    };

let functionResult = counter(["shaun", "sara", "jessica"]);

You need to return the result to a variable.您需要将结果返回给一个变量。 Then you can use it as you want.然后你可以随意使用它。

So, you are actually returning something, you just have no way to view it without console.log() if you're wanting to use the returns of this function in another function, or somewhere else, you can assign it to a variable like所以,你实际上是在返回一些东西,如果你想在另一个函数或其他地方使用这个函数的返回值,你就没有办法在没有 console.log() 的情况下查看它,你可以将它分配给一个变量,如

const myCounter = counter(["shaun", "sara", "jessica"])
console.log(myCounter)

All return is doing is making the results of the function available to be used elsewhere. return 所做的一切都是使函数的结果可用于其他地方。 If you aren't displaying it via console.log, or some other method (putting it into an HTML element or something) then you'll never "see" it anywhere, and it'll looks like the function isn't doing anything, even though it is.如果您不通过 console.log 或其他一些方法(将其放入 HTML 元素或其他内容)显示它,那么您将永远不会在任何地方“看到”它,并且看起来该函数没有执行任何操作,即使是。

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

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