简体   繁体   English

在不重新设置javascript中变量的情况下,在for循环中重用函数的正确方法是什么?

[英]What is the correct way of reusing a function in for loop without resetting variables in javascript?

I came across this problem when I was trying to optimize my code. 当我尝试优化代码时遇到了这个问题。 I am trying to use a function in my code (seems like I have to repeat code 2 times to make it work, because I am not getting the result I want): 我正在尝试在代码中使用一个函数(好像我必须重复执行两次代码才能使其正常工作,因为我没有得到想要的结果):

 let tmpCont = undefined; let tmpPrice = undefined; let evaluate = undefined; let evaluate2 = undefined; let cont = 0; let tmp = true; let cont2 = 30; let tmp2 = false; for (let i = 0; i < 10; i++) { evaluate = analize(cont, tmp); console.log(evaluate + "test"); evaluate2 = analize(cont2, tmp2); console.log(evaluate2); } function analize(tmpCont, tmpPrice) { for (let i = 0; i < 10; i++) { tmpCont++; } return [tmpCont, tmpPrice]; } 

Result I am getting (what I dont want): 结果我得到(我不想要的):

10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]

My question is: Am I doing something wrong? 我的问题是:我做错什么了吗? I know why I am getting the result I dont want and I think something is missing BUT do i have to repeat code to get the result below? 我知道为什么我得到了我不想要的结果,但我认为有些东西丢失了, 但是我必须重复代码才能得到以下结果吗?

Result I want: 结果我想要:

10,truetest
[ 40, false ]
20,truetest
[ 50, false ]
30,truetest
[ 60, false ]
40,truetest
[ 70, false ]
50,truetest
[ 80, false ]
60,truetest
[ 90, false ]
70,truetest
[ 100, false ]
80,truetest
[ 110, false ]
90,truetest
[ 120, false ]
100,truetest
[ 130, false ]

In javascript all variables passed into a function are passed by value not by reference. 在javascript中,传递给函数的所有变量均按值传递,而不是按引用传递 Changing the value in a function will not change the value of a variable passed. 更改函数中的值不会更改传递的变量的值。 You have 2 options: 您有2个选择:

  1. Pass an object containing your counts. 传递包含计数的对象。
  2. Assign the returned values. 分配返回的值。

so to do the first: 要做第一个:

 let evaluate = undefined; let evaluate2 = undefined; const values={cont:0, price:true}; const values2={cont:30, price:false}; for (let i = 0; i < 10; i++) { evaluate = analize(values); console.log(evaluate + " test"); evaluate2 = analize(values2); console.log(evaluate2); } function analize(tmp) { for (let i = 0; i < 10; i++){ tmp.cont++; } return [tmp.cont, tmp.price]; } 

or: 要么:

 let evaluate = undefined; let evaluate2 = undefined; let cont = 0; let tmp = true; let cont2 = 30; let tmp2 = false; for (let i = 0; i < 10; i++) { [cont,tmp] = analize(cont, tmp); console.log([cont,tmp] + " test"); [cont2, tmp2]= analize(cont2, tmp2); console.log([cont2, tmp2]); } function analize(tmpCont, tmpPrice) { for (let i = 0; i < 10; i++) { tmpCont++; } return [tmpCont, tmpPrice]; } 

You could just increment the values at the end of each for loop 您可以在每个for循环的末尾增加值

 let tmpCont = undefined; let tmpPrice = undefined; let evaluate = undefined; let evaluate2 = undefined; let cont = 0; let tmp = true; let cont2 = 30; let tmp2 = false; for (let i = 0; i < 10; i++) { evaluate = analize(cont, tmp); console.log(evaluate + "test"); evaluate2 = analize(cont2, tmp2); console.log(evaluate2); cont += 10 // this cont2 += 10 // and this } function analize(tmpCont, tmpPrice) { for (let i = 0; i < 10; i++) { tmpCont++; } return [tmpCont, tmpPrice]; } 

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

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