简体   繁体   English

Return 在这里做什么?

[英]What does Return do in here?

I have problem with 'return' means in this code.我对这段代码中的“返回”方式有疑问。

1. 1.

 function func4() { var str = "function works."; console.log(str); } func4();

2. 2.

 function func4() { var str = "function works."; return str; } var value = func4(); console.log(value);

Both of them, their result is 'function works.'.他们俩,他们的结果都是'功能有效'。 I know that return used for exit function but I'm still confuse when I have to use return exactly.我知道 return 用于退出 function 但是当我必须完全使用 return 时我仍然感到困惑。 Sorry about my super basic question:(对不起我的超级基本问题:(

As far as I understand 'return' assigns value to a function and returns it, so you're displaying function's value.据我了解,'return' 将值分配给 function 并返回它,因此您正在显示函数的值。 In the first case you are just simply invoking a function to display a string.在第一种情况下,您只是简单地调用 function 来显示字符串。

Let's analize this two scenarios:让我们分析这两种情况:

  1. You have a function that initialize a variable with a predefinided value, and then, you log the value.您有一个 function 使用预定义值初始化变量,然后记录该值。 Then, outside the function you execute it然后,在 function 之外执行它

  2. You have the same variable but with the difference that instead of loggin the value inside the function, you returned it from it.您有相同的变量,但不同之处在于,不是登录 function 中的值,而是从它返回它。 So you can initialize the funcion and store the value on another variable var value = func4();所以你可以初始化函数并将值存储在另一个变量var value = func4(); . .

Let me try to explain it with some requirements.让我试着用一些要求来解释它。

  1. I want a function which returns me some data instead of passing a variable and updating the variable in function.我想要一个 function,它返回一些数据而不是传递变量并更新 function 中的变量。

  2. You call a function and it is always best to test negative scenarios first.你打电话给 function,最好先测试负面情况。 So in case of negative scenario you can return it from there it self.因此,如果出现负面情况,您可以从那里自行返回。

In your second case if you see you are getting a value from that function and then printing it.在第二种情况下,如果您看到您从 function 中获取值,然后打印它。 Same thing you can not do using first function.使用第一个 function 不能做同样的事情。

Always there are workarounds for everything.总是有解决方法。 In the end it depends on your need and what is best suited for that situation.最后,这取决于您的需求以及最适合这种情况的方法。

Both of those functions don't equal the same thing, but they do log the same string.这两个函数不等于同一件事,但它们确实记录了相同的字符串。

func4() in #1 is equal to undefined , because it returns nothing. #1 中的func4()等于undefined ,因为它不返回任何内容。

func4() in #2 returns (gives back) the value "function works." #2 中的func4()返回(返回)值"function works." , a string, which is then given to console.log outside of the function. ,一个字符串,然后将其提供给 function之外的console.log

 function func1() { var str = "function works."; // console.log(str); } func1(); function func2() { var str = "function works."; return str; } // console.log(func2()); console.log(func1() === undefined); console.log(func2() === 'function works.');

If you want to use the func4() value for further calculations without calling it again, then you would return {value}.如果您想使用 func4() 值进行进一步计算而不再次调用它,那么您将返回 {value}。

For eg例如

function func4(userInput) {
    return userInput % 2 == 0;
}


var response = func4(userInput);

if(response == true) {
    console.log('user entered an even number');
} else {
    console.log('user entered a odd number');
}

// from here you can use the value of response n times without calling the function again.

Whereas, if you don't return then you will have to call the function x number of times whenever you want to re-user the response of it.然而,如果您不返回,那么只要您想重新使用它的响应,就必须调用 function x 次。

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

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