简体   繁体   English

为什么在 while 循环中声明变量时结果会改变?

[英]Why does the outcome change when the variable is declared within the while loop?

I have two simple functions here.我这里有两个简单的功能。 In the loop function I have defined facky as one at the top of the function. Two things I do not understand:loop function 中,我将 facky 定义为 function 顶部的一个。有两件事我不明白:

  1. The answer to console.log(loop(5)) is 120 when facky is defined at the top of loopfacky定义在loop的顶部时, console.log(loop(5))的答案是 120
  2. When I move var facky = 1;当我移动时var facky = 1; within the while loop, the answer is 2. I understand why this is two.在 while 循环中,答案是 2。我明白为什么这是两个。 What I don't understand is why the behavior is different when the variable is outside?我不明白的是为什么当变量在外部时行为会有所不同?

 function loop(size) { while (size > 1) { var facky = 1; facky = facky * size; size = size - 1; } clunk(facky); } function clunk(times) { var num = times; while (num > 0) { console.log("clunk"); num = num - 1; } } loop(5);

In your while loop, whenever the loop iterates, facky is reset to 1, so it will only print twice because the last iteration of the while loop multiplies facky by 2.在您的 while 循环中,每当循环迭代时, facky都会重置为 1,因此它只会打印两次,因为 while 循环的最后一次迭代将facky乘以 2。

When you move the declaration outside of the while loop, facky does not reset after every iteration and takes on the value of 5!当您将声明移到 while 循环之外时, facky不会在每次迭代后重置并取值5! , or 120 after the final iteration. ,或最后一次迭代后的120

You shouldn't declare a variable inside a loop.您不应该在循环内声明变量。 Try this instead:试试这个:

function loop(size) {
  
  var facky = 1;

  while (size > 1) {
    facky = facky * size;
    size = size - 1;
  }

  clunk(facky);

}

It doesn't really matter where you declare the var facky , it's always function-scoped.在哪里声明var facky重要,它始终是函数范围的。

What matters is whether you reset facky = 1 in every iteration of your loop, or whether you only initialise it once before the loop.重要的是你是否在循环的每次迭代中重置facky = 1 ,或者你是否只在循环之前初始化它一次。

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

相关问题 为什么我的外部变量在使用 while 循环时不会改变? - Why does my outer variable not change when using a while loop? 在while循环中,模数while循环会产生不同的结果吗? - Does modulus while loop have a different outcome when in while loop? 当条件检查变量变为0时,为什么while循环停止? - Why does the while loop stop when condition check variable becomes 0? 为什么 google 变量从未被声明时存在? - Why does google variable exist when it has never been declared? Javascript:在箭头 function 内声明的变量无法在 while 循环内更新 - Javascript: variable declared inside arrow function can't be updated within while loop Angular无法识别forEach循环中范围变量的变化 - Angular does not recognize scope variable change within forEach loop 当我声明为变量的变量不等于0时,为什么变量变为0? - Why does my variable turn into 0 when what I declared as that variable does not equal 0? 为什么在for循环中数组的顺序保持变化 - Why does the order of my array keep change while in a for loop Javascript从循环内更改循环变量 - Javascript change for loop variable from within the loop 为什么 jest.fn().mockImplementation 在 beforeEach 范围中声明时看不到 reset 变量? - Why does jest.fn().mockImplementation not see the reset variable when it's declared in the beforeEach scope?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM