javascript 何时使用 return,返回的数据会怎样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7187114/
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
When to use return, and what happens to returned data?
提问by ilyo
What is the difference between:
有什么区别:
function bla1(x){console.log(x)}
and
和
function bla(x){return console.log(x)}
In which cases should I use return
?
我应该在哪些情况下使用return
?
also, when a value is returned from a function, what happens to it? is it stored somewhere?
另外,当一个函数返回一个值时,它会发生什么?它存储在某个地方吗?
回答by Felix Kling
What is the difference
有什么区别
The first function returns undefined
(as it does not return
anything explicitly), the second one returns whatever console.log
returns.
第一个函数返回undefined
(因为它没有return
显式地执行任何操作),第二个函数返回任何console.log
返回值。
In which cases should I use return?
在什么情况下我应该使用 return ?
When the function is generating some value and you want to pass it back to the caller. Take Math.pow
for example. It takes two arguments, the base and the exponent and returns the base raised to the exponent.
当函数生成某个值并且您想将其传回给调用者时。以Math.pow
为例。它接受两个参数,基数和指数,并返回升至指数的基数。
When a value is returned from a function, what happens to it? is it stored somewhere?
当一个函数返回一个值时,它会发生什么?它存储在某个地方吗?
If you want to store the return value, then you have to assign it to a variable
如果要存储返回值,则必须将其分配给变量
var value = someFunction();
This stores the return value of someFunction
in value
. If you call the function without assigning the return value, then the value is just silently dropped:
这存储了someFunction
in的返回值value
。如果您在不分配返回值的情况下调用该函数,则该值将被静默删除:
someFunction();
These are programming basics and are not only relevant to JavaScript. You should find a book which introduces these basics and in particular for JavaScript, I recommend to read the MDN JavaScript Guide. Maybe the Wikipedia article about Functionsis helpful as well.
这些是编程基础知识,不仅与 JavaScript 相关。您应该找到一本介绍这些基础知识的书,尤其是 JavaScript,我建议您阅读MDN JavaScript 指南。也许维基百科关于函数的文章也有帮助。
回答by jamietelin
Return in a function is a way to pass back data from the function.
函数中的返回是从函数传回数据的一种方式。
Example:
例子:
function test(){
var test = 1+1;
return test;
}
var feedback = test(); //feedback would now contain the value 2 if outputted.
We could also send a variable into the function and then return it back out.
我们也可以将一个变量发送到函数中,然后将其返回。
Example 2:
示例 2:
function test(i){
i= i+1;
return i;
}
var feedback = test(1); //feedback would also output the value 2.
回答by halbherz
As you already mentioned, return gives you the possibility to call a function and save its returning value.
正如您已经提到的, return 使您可以调用函数并保存其返回值。
Here is a little example:
这是一个小例子:
function bla(x) { return "blablabla"; }
If I call the method I will get a string with blablabla:
如果我调用该方法,我将得到一个带有 blablabla 的字符串:
var blastring = bla(x);
alert(blastring);
This would result in an alert with blablabla.
这将导致带有 blablabla 的警报。
回答by Lajos Arpad
With return
you specify what the value of a function
is. You can use this value to do further operations or to store it into a variable and so on.
由return
您指定 a 的值function
是什么。您可以使用此值进行进一步的操作或将其存储到变量中等等。
Since console.log
return
s undefined
, the examples in your question are equivalent, as a function
not reaching a return statement will return undefined
as well. But let me give you an example:
由于console.log
return
s undefined
,您问题中的示例是等效的,因为function
未到达 return 语句也会返回undefined
。但是让我给你举个例子:
function sum(arr) {
var s = 0;
for (var index in arr) {
s += arr[index];
}
return s;
}
function prodsum(arr, scalar) {
return scalar * sum(arr);
}
console.log(prodsum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3));
The result will be 165. If we remove the return
s, then both function
s will return
undefined
:
结果将是 165。如果我们删除return
s,那么两个function
s 都会return
undefined
:
function sum(arr) {
var s = 0;
for (var index in arr) {
s += arr[index];
}
s;
}
function prodsum(arr, scalar) {
scalar * sum(arr);
}
console.log(prodsum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3));
and the result will be undefined
as well. Basically, if you want the function
to have a conclusion or final value, then you have a return
in it.
结果也会如此undefined
。基本上,如果您希望function
有一个结论或最终值,那么您就有一个return
。
回答by brahmaji tammana
If you just add return;
in the function. It stops the execution of the function.
如果你只是return;
在函数中添加。它停止函数的执行。