简体   繁体   English

了解return语句。

[英]Understanding the return statement.

I have looked at many examples but still cant figure how the return statement works. 我看了很多例子,但仍然无法弄清楚return语句是如何工作的。

 function One() {
   var newVal = 0;
   Too();
   console.log(newVal);
 }

 function Too(newVal) {
   ++newVal; 
   return(newVal);
 }

Shouldn't this print 1 to the console? 此打印不应该在控制台上打印1吗? What I'm really trying to do is to have function Too increase newVal by 1 every time it's called. 我真正想做的是让函数TooVal每次调用都增加1。 But I cant even figure out how to get the return statement to work. 但是我什至无法弄清楚如何使return语句起作用。 I should mention I don't want to use global variables. 我应该提到我不想使用全局变量。 Thanks for any help. 谢谢你的帮助。

Shouldn't this print 1 to the console? 此打印不应该在控制台上打印1吗?

No. The newVal inside Too is not the newVal inside One . 号的newVal里面Too不是newVal里面One They're completely separate. 他们是完全分开的。

What I'm really trying to do is to have function Too increase newVal by 1 every time it's called. 我真正想做的是让函数TooVal每次调用都增加1。

It can't, JavaScript doesn't have any mechanism for passing variables by reference like some other languages do (C#'s ref and out arguments, for instance). 不能,JavaScript没有像其他语言一样通过引用传递变量的任何机制(例如C#的refout参数)。 The closest you can come is to pass in a reference to an object and modify the object's state (which is really quite different and I won't go into it here as it would confuse things :-) ). 最接近的方法是传递对对象的引用并修改对象的状态(这实际上是完全不同的,在这里我不会赘述,因为它会使事情变得混乱:-))。

None of which has anything to do with the return statement. 这些都与return语句无关。

The simplest way to do what you were describing is this: 做您所描述的最简单的方法是:

function One() {
    var newVal = 0;
    newVal = Too(newVal);
    console.log(newVal);
}

function Too(arg) {
    ++arg; 
    return arg;
}

Here's what happens when we call One : 当我们调用One时,会发生以下情况:

  1. A local variable called newVal is created. 创建一个名为newVal的局部变量。
  2. Its value is set to 0 . 其值设置为0
  3. A copy of its value is passed into the function Too as an argument. 的副本作为参数传递到函数Too中。 This has no link back to the newVal variable. 没有链接返回到newVal变量。
  4. Too is called, accepting that value in its arg argument (arguments in JavaScript are effectively local variables). 也会调用Too并在其arg参数中接受该值(JavaScript中的参数实际上是局部变量)。
  5. Too increments the value in arg . arg的值也增​​加Too
  6. Too returns a copy of the value held by arg as its return value . Too返回arg保留的值的副本作为其返回值
  7. The return value of Too is assigned to the variable newVal . Too的返回值分配给变量newVal
  8. We output that value (by passing it into console.log ). 我们输出该值(通过将其传递到console.log )。

No, it shouldn't. 不,不应该。 The return statement establishes what the value of the function invocation will be in the calling environment when control returns there. return语句确定当控制返回到调用环境时,函数调用的值在调用环境中将是什么。 Since your calling environment doesn't use the return value, there's no net effect. 由于您的调用环境不使用返回值,因此没有净效果。

Here's how you'd get the value back: 这是您获取价值的方法:

newVal = Too(newVal);

If you want to make a function that acts as a counter, such that each time you call it you get a new number back, you can do something like this: 如果要创建一个用作计数器的函数,这样每次调用它时都会得到一个新的数字,您可以执行以下操作:

var counter = function(initial) {
  var c = initial || 0;
  return function() {
    return c++;
  };
}(0);

What that does is use an anonymous function to set up a persistent environment for another function, which is returned from the outer one when it's (immediately) invoked. 这样做是使用匿名函数为另一个函数设置一个持久环境,该环境在(立即)被调用时从外部函数返回。 The returned function can then be called to return a new value from its private counter (the variable "c"): 然后可以调用返回的函数,以从其专用计数器(变量“ c”)返回新值:

var currentCount = counter();
alert(currentCount); // 0, the first time
currentCount = counter();
alert(currentCount); // 1, and so on

If a function return a value you need to call that function in your console.log or capture the returned value. 如果函数返回一个值,则需要在console.log调用该函数或捕获返回的值。

function One() {
   var newVal = 0;

   console.log(Too(newVal));
 }

 function Too(newVal) {
   ++newVal; 
   return(newVal);
 }

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

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