卡在一个循环中 [英] stuck in a loop

查看:65
本文介绍了卡在一个循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


希望有人能看到这里有什么问题我有以下功能


function RemoveMenuFromHoldArray(menuName){

var i = 0;

for(i = 0; i< = MenusToHoldOpen.length-1; i ++){

if(MenusToHoldOpen [i ] == menuName){

MenusToHoldOpen.splice(i,1);

返回;

}

}

}

我通过反复试验发现,如果我没有var i

= 0;代码中的一行,函数卡在一个循环中。


任何人都可以告诉我原因,或逻辑错误的地方?

解决方案

< re ************ @ hotmail.com>在消息中写道

新闻:ea ****************************** @ news.teranew s。 com ...

< snip>

function RemoveMenuFromHoldArray(menuName){
var i = 0;
for(i = 0; i< ; = MenusToHoldOpen.length-1; i ++){
if(MenusToHoldOpen [i] == menuName){
MenusToHoldOpen.splice(i,1);
return;
}
}
}

我通过反复试验发现,如果我没有var i = 0;代码中的一行,函数卡在一个循环中。

任何人都可以告诉我为什么,或逻辑错误的地方?




包含var i = 0的区别;并且省略它是

- var - 声明我成为(函数)局部变量和

没有它我是全局的。良好的编程习惯说永远不会给出比它需要的更多范围的

变量,所以在这种情况下我应该是本地的。


但对于上面的函数应该是对

函数的运行方式没有任何影响,因为改变i的唯一代码是初始化

为零和后增量运算符。所以我应该总是小于

或等于(MenusToHoldOpen.length-1),并且随着我变得更大它

最终会变得大于(MenusToHoldOpen。 length-1)和

循环应终止。


全局变量作为循环计数器的不当使用经常变成

a问题如果循环的主体使用具有相同标识符的全局变量调用另一个本身

的函数。但是在上面的代码中,

唯一的函数调用(以及因此看不见的代码)是

MenusToHoldOpen.splice(i,1)调用,并假设MenusToHoldOpen是

一个数组和splice是Array.prototype.splice,splice应该是一个

本机代码函数,不与任何全局变量交互。


顺便说一句,(i< =(MenusToHoldOpen.length-1))应该与(i

< MenusToHoldOpen.length)相同,但不要求每次测试都减去。

我还会使用 - break; - 终止 - for - 循环的声明

而不是 - 返回。


Richard。


2003年10月30日星期四15:33:27 -0000,Richard Cornford

< Ri ***** @ litotes.demon.co.uk>写道:

< re ************ @ hotmail.com>在消息中写道
新闻:ea ****************************** @ news.terane ws.com ...
< snip>

function RemoveMenuFromHoldArray(menuName){
var i = 0;
for(i = 0; i< = MenusToHoldOpen.length-1; i ++){
if(MenusToHoldOpen [i] == menuName){
MenusToHoldOpen.splice(i,1);
return;
}
}
}

我通过反复试验发现如果我没有var i = 0;在代码行中,函数卡在一个循环中。

任何人都可以告诉我为什么,或逻辑错在哪里?



包括var i = 0;并且省略它是用
- var - 声明我成为(函数)局部变量而没有它我是全局的。良好的编程习惯说永远不要给变量提供超出其需要的范围,所以在这种情况下我应该是本地的。

但对于上面的函数,应该没有区别
函数作为唯一改变i的代码是初始化零和后增量运算符。所以我应该总是小于
或等于(MenusToHoldOpen.length-1),并且随着我变大,它最终会变得比(MenusToHoldOpen.length-1)和
如果循环体调用另一个本身使用全局的函数,则全局变量作为循环计数器的不恰当使用通常会成为一个问题。具有相同标识符的变量。但是在上面的代码中,唯一的函数调用(以及因此看不见的代码)是调用MenusToHoldOpen.splice(i,1),并且假设MenusToHoldOpen是一个数组并且拼接是Array.prototype.splice,splice应该是一个原生代码函数,不与任何全局变量交互。

顺便说一句,(i< =(MenusToHoldOpen.length-1))应该与(i
< MenusToHoldOpen.length)相同,但不要求每次测试都减法。
我也会使用 - break; - 声明终止 - for - 循环
而不是 - 返回。

理查德。




正确。


所以,如果我正确地理解这一点,那些我有的功能

包含一个基于变量i的循环,将全部引用

相同的变量,如果它没有在每个函数的本地声明。


这自然会导致该变量的值发生冲突,

特别是如果带循环的函数使用

循环调用其他函数。


正确吗?

因此我会确保我的所有循环变量都在本地声明为

避免这个问题(假设我是对的)。


< ;再************ @ hotmail.com>在消息中写道

新闻:eb ****************************** @ news.teranew s。 com ...

< snip>

...永远不会给变量提供比它需要更多的范围。 ..


< snip>所以,如果我正确理解这一点,那些包含基于变量i的循环的函数将全部

是的。

这自然会导致该变量值的冲突,特别是如果带循环的函数调用另一个
带循环功能。

正确吗?


是的。

因此我会确保我的所有循环变量都在本地声明
以避免这个问题(假设我是右边)。




永远不要给变量提供超出需要的范围。


Richard。


Hi All,

hope someone can see what wrong here I have the following function

function RemoveMenuFromHoldArray(menuName) {
var i = 0;
for (i=0;i<=MenusToHoldOpen.length-1;i++) {
if (MenusToHoldOpen[i] == menuName) {
MenusToHoldOpen.splice(i,1);
return;
}
}
}
I have found through trial and error that if I do not have the "var i
= 0;" line in the code, the function gets stuck in a loop.

can anyone tell me why , or where the logic is wrong?

解决方案

<re************@hotmail.com> wrote in message
news:ea******************************@news.teranew s.com...
<snip>

function RemoveMenuFromHoldArray(menuName) {
var i = 0;
for (i=0;i<=MenusToHoldOpen.length-1;i++) {
if (MenusToHoldOpen[i] == menuName) {
MenusToHoldOpen.splice(i,1);
return;
}
}
}
I have found through trial and error that if I do not have
the "var i = 0;" line in the code, the function gets stuck in a loop.

can anyone tell me why , or where the logic is wrong?



The difference between including var i = 0; and omitting it is that with
the - var - declaration i becomes a (function) local variable and
without it i is global. Good programming practice says never give a
variable more scope than it needs, so in this case i should be local.

But for the function above that should make no difference to the way the
function operates as the only code that alters i is the initialisation
to zero and the post increment operator. So i should always be less than
or equal to (MenusToHoldOpen.length-1) or not, and as i gets bigger it
should eventually become bigger than (MenusToHoldOpen.length-1) and the
loop should terminate.

The inappropriate use of global variables as loop counters often becomes
a problem if the body of a loop calls another function that is itself
using a global variable with the same identifier. But in the code above
the only function call (and therefor unseen code) is the
MenusToHoldOpen.splice(i, 1) call, and, assuming that MenusToHoldOpen is
an Array and splice is Array.prototype.splice, splice should be a
native-code function and not interact with any global variables.

Incidentally, (i <= (MenusToHoldOpen.length-1)) should be the same as (i
< MenusToHoldOpen.length) but not require the subtraction on each test.
Also I would use the - break; - statement to terminate the - for - loop
instead of - return.

Richard.


On Thu, 30 Oct 2003 15:33:27 -0000, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> wrote:

<re************@hotmail.com> wrote in message
news:ea******************************@news.terane ws.com...
<snip>

function RemoveMenuFromHoldArray(menuName) {
var i = 0;
for (i=0;i<=MenusToHoldOpen.length-1;i++) {
if (MenusToHoldOpen[i] == menuName) {
MenusToHoldOpen.splice(i,1);
return;
}
}
}
I have found through trial and error that if I do not have
the "var i = 0;" line in the code, the function gets stuck in a loop.

can anyone tell me why , or where the logic is wrong?



The difference between including var i = 0; and omitting it is that with
the - var - declaration i becomes a (function) local variable and
without it i is global. Good programming practice says never give a
variable more scope than it needs, so in this case i should be local.

But for the function above that should make no difference to the way the
function operates as the only code that alters i is the initialisation
to zero and the post increment operator. So i should always be less than
or equal to (MenusToHoldOpen.length-1) or not, and as i gets bigger it
should eventually become bigger than (MenusToHoldOpen.length-1) and the
loop should terminate.

The inappropriate use of global variables as loop counters often becomes
a problem if the body of a loop calls another function that is itself
using a global variable with the same identifier. But in the code above
the only function call (and therefor unseen code) is the
MenusToHoldOpen.splice(i, 1) call, and, assuming that MenusToHoldOpen is
an Array and splice is Array.prototype.splice, splice should be a
native-code function and not interact with any global variables.

Incidentally, (i <= (MenusToHoldOpen.length-1)) should be the same as (i
< MenusToHoldOpen.length) but not require the subtraction on each test.
Also I would use the - break; - statement to terminate the - for - loop
instead of - return.

Richard.



Right.

So if I''m understanding this correctly, those function I have that
contain a loop based on a variable called "i", will all reference the
same variable if it is not declared locally to each function.

Which would naturally enough cause clashes on value of that variable,
especially if a function with a loop calls a nother function with a
loop.

Correct?
Thus I''ll make sure all my loop variables are declared locally to
avoid this problem (assuming I''m right).


<re************@hotmail.com> wrote in message
news:eb******************************@news.teranew s.com...
<snip>

... never give a variable more scope than it
needs, ...

<snip>So if I''m understanding this correctly, those function I have
that contain a loop based on a variable called "i", will all
reference the same variable if it is not declared locally to
each function.
yes.
Which would naturally enough cause clashes on value of that
variable, especially if a function with a loop calls another
function with a loop.

Correct?
Yes.
Thus I''ll make sure all my loop variables are declared
locally to avoid this problem (assuming I''m right).



Never give a variable more scope than it needs.

Richard.


这篇关于卡在一个循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆