简体   繁体   English

Javascript while循环返回值

[英]Javascript while loop return value

I have a simple question regarding while loop in Javascript. 我有一个简单的问题关于Javascript中的while循环。 When I run this simple loop in browser console: 当我在浏览器控制台中运行这个简单的循环:

 var count = 0; while (count < 10) { console.log(count); count++; } 

The output to console log is 0,1,2...9. 控制台日志的输出为0,1,2 ... 9。 (as expected) . (如预期的那样) However there is one more number which is returned to the console: 但是还有一个号码返回到控制台:

<- 9

Where does this return value come from? 这个回报值来自哪里?

I assume this is the return value of count++ expression . 我假设这是count++ expression的返回值。 But why is the value not returned by every single loop? 但是为什么每个循环都没有返回值?

Is it possible to somehow catch that returned value to a variable? 有可能以某种方式将返回的值捕获到变量中吗?

Read-eval-print-loops (REPLs) like browser consoles show the last result that the code generated. 像浏览器控制台这样的read-eval-print-loops(REPL)显示代码生成的最后结果。 Somewhat surprisingly, JavaScript while loops and blocks have a result. 有点令人惊讶的是,JavaScript while循环和块有结果。 For blocks, it's the result of the last statement in the block. 对于块,它是块中最后一个语句的结果。 For while statements, it's the result of the last iteration of the statement attached to the while (a block in your case). 对于while语句,它是附加到while的语句的最后一次迭代的结果(在您的情况下是一个块)。

Here's a simpler example with just a block: 这是一个只有一个块的简单示例:

{1 + 1; 3 + 3;}

In a REPL like the browser console, the above will show you 6, because it's a block containing two ExpressionStatements . 在像浏览器控制台这样的REPL中,上面将显示6,因为它是一个包含两个ExpressionStatements的块。 The result of the first ExpressionStatement is 2 (that result is thrown away), and the result of the second one is 6, so the result of the block is 6. This is covered in the specification : 第一个ExpressionStatement的结果是2(结果被抛弃),第二个的结果是6,所以块的结果是6.这在规范中有所涉及

  1. Let blockValue be the result of evaluating StatementList . blockValue成为评估StatementList的结果。
  2. Set the running execution context's LexicalEnvironment to oldEnv. 将正在运行的执行上下文的LexicalEnvironment设置为oldEnv。
  3. Return blockValue . 返回blockValue

while loop results are covered here . while循环结果在这里介绍

Is it possible to somehow catch that returned value to a variable? 有可能以某种方式将返回的值捕获到变量中吗?

No, these results are not accessible in the code. 不,代码中无法访问这些结果。 Eg, you can't do var x = while (count < 10 { count++; }); 例如,你不能做var x = while (count < 10 { count++; }); or similar. 或类似的。 You'd have to capture the result you want inside the loop/block/etc., assigning it to a variable or similar. 你必须捕捉到你想要的内部循环/块/等结果,将其分配给一个变量或类似的。 Or (and I'm not suggesting this), use eval as Alin points out . 或者(我建议这样做),使用eval 作为Alin指出

When you run code directly in the browser console, it runs the code then logs the value of the last expression executed, in this case the value of count++ , which at the final execution is 9 (it gets changed to 10 with the post-increment operator, ie after the value 9 is "read"). 当您直接在浏览器控制台中运行代码时,它会运行代码,然后记录执行的最后一个表达式的值,在本例中为count++的值,在最终执行时为9 (后续增量更改为10操作者,即后的值9是“读出”)。

If you changed your code to: 如果您将代码更改为:

var count = 0;
while (count < 10) {
  console.log(count);
  ++count;
}

It would instead log <- 10 . 它会改为记录<- 10

But why is the value not returned by every single loop? 但是为什么每个循环都没有返回值?

Because it's not being returned at all, it's just the behaviour of the console. 因为它根本没有被返回,所以它只是控制台的行为。

Is it possible to somehow catch that returned value to a variable? 有可能以某种方式将返回的值捕获到变量中吗?

Yes, but you'll need to assign it: 是的,但你需要分配它:

var count = 0;
var lastcount;
while (count < 10) {
  console.log(count);
  lastcount = count++;
}

console.log(lastcount);

Note that if you run this snippet in the console, you'll get two 9 s logged (one from the final loop, one from the extra console.log ) followed by <- undefined , because the last console.log returns undefined . 请注意,如果您在控制台中运行此代码段,则会记录两个9秒(一个来自最终循环,一个来自额外的console.log ),然后是<- undefined ,因为最后一个console.log返回undefined

I think the answer can be found in the behavior of eval : 我认为答案可以在eval行为中找到:

eval() returns the value of the last expression evaluated. eval()返回最后计算的表达式的值。

What you throw in the console is most probably put through an eval() (or something similar). 你在控制台中抛出的东西最有可能通过eval() (或类似的东西)。 And the last evaluated expression, in your case count++ is returned and then logged to the console. 最后评估的表达式,在您的情况下count++返回,然后记录到控制台。 (the value is 9 , and not 10 , because of post-incrementation). (值为9 ,而不是10 ,因为后增量)。

I don't think you can store the result of the evaluation, unless you explicitly put it through another eval() . 我不认为你可以存储评估结果,除非你明确地通过另一个eval()

var x = eval("var count = 0;while (count < 10) {console.log(count);count++;};");

Or, in the Google Chrome console, you can use $_ . 或者,在Google Chrome控制台中,您可以使用$_

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

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