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? 12 4867
<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.
On Fri, 31 Oct 2003 19:25:20 -0000, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> wrote: <re************@hotmail.com> wrote in message news:eb******************************@news.terane ws.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.
cool, glad I got that sorted. For some reason I thought that I
remembered javascript creating the required variable for the loop, and
keeping it local, but maybe I was thinking of something else.
I'm surpeised that it does it that way actually, as it could be very
dangerous, as I have found out the hard way.
<re************@hotmail.com> wrote in message
news:ae******************************@news.teranew s.com...
<snip> ... . For some reason I thought that I remembered javascript creating the required variable for the loop, and keeping it local, but maybe I was thinking of something else.
A normal formulation of a - for - statement would be along the lines
of:-
for(var c = 0;c < x;c++){
...
}
Placing the - var - in the - for - statement. But because JavaScript
identifies - var - when it initially reads the code, and creates a local
variable for each - var - it has found within a function body as the
program enters the execution context of a call to that function, placing
the - var - in the first expression of the - for - statement has exactly
the same effect of using - var - to declare a local variable at the
start (or even at the end) of a function body. Habitually declaring the
counter of a - for - loop with - var - in the first expression just
makes it difficult for the counter to accidentally be global.
(JavaScript does not have a problem if you declare the same identifier
as a local variable repeatedly, it only creates on variable for each
name used.)
I'm surpeised that it does it that way actually,
What would be the alternative? The interpreter couldn't second-guess the
intention of the programmer. Even an effective AI would struggle to get
that right all of the time and otherwise the decisions would have to be
rule based, as they are now.
as it could be very dangerous, as I have found out the hard way.
You can shoot yourself in the foot in any programming language.
Understanding what you are doing is the best defence, and for
understanding the behaviour of JavaScript the language specification
(ECMA 262) is the best guide.
Richard. re************@hotmail.com said: cool, glad I got that sorted. For some reason I thought that I remembered javascript creating the required variable for the loop, and keeping it local, but maybe I was thinking of something else.
I'm surpeised that it does it that way actually, as it could be very dangerous, as I have found out the hard way.
It sounds like you're thinking of loop variables as being
somehow different from other variables. That's not true.
All variables are subject to being modified by function
calls unless they're declared locally, so all variables
should be declared where they are used.
On 1 Nov 2003 09:41:52 -0800, Lee <RE**************@cox.net> wrote: re************@hotmail.com said:
cool, glad I got that sorted. For some reason I thought that I remembered javascript creating the required variable for the loop, and keeping it local, but maybe I was thinking of something else.
I'm surpeised that it does it that way actually, as it could be very dangerous, as I have found out the hard way.
It sounds like you're thinking of loop variables as being somehow different from other variables. That's not true.
All variables are subject to being modified by function calls unless they're declared locally, so all variables should be declared where they are used.
I wasn't thinking of them being special as such, just I thought that
if it wasn't an already declared variable then it would be created
locally, not globally. That obvioulsy isn't the case.
I may have gotten confused with another loagnauge I was looking at a
while back, but If I was I can't remember which language.
Oh the things that you really miss from working with a compiliable
language, like syntax checking and scope checking etc. re************@hotmail.com said: I wasn't thinking of them being special as such, just I thought that if it wasn't an already declared variable then it would be created locally, not globally. That obvioulsy isn't the case.
I may have gotten confused with another loagnauge I was looking at a while back, but If I was I can't remember which language.
Oh the things that you really miss from working with a compiliable language, like syntax checking and scope checking etc.
Most compiled languages require you to declare all variables.
The fact that Javascript *doesn't* is what got you into
trouble.
Scope and syntax checking aren't much use if a typo can
result in the creation of a new variable.
<re************@hotmail.com> wrote in message
news:fe******************************@news.teranew s.com...
<snip> ... , just I thought that if it wasn't an already declared variable then it would be created locally, not globally. That obvioulsy isn't the case.
Yes, I suppose the decision could have gone either way. It probably
seemed easier, having traversed the scope chain to the global object in
order to determine whether the identifier could be resolved to just add
it as a property to that object rather than going back to the variable
object in the execution context.
<snip>Oh the things that you really miss from working with a compiliable language, like syntax checking and scope checking etc.
You might find Douglas Crokford's JSLINT program useful in that
context:-
<URL: http://www.crockford.com/javascript/jslint.html >
Richard.
"Richard Cornford" <ri*****@litotes.demon.co.uk> writes: Yes, I suppose the decision could have gone either way. It probably seemed easier, having traversed the scope chain to the global object in order to determine whether the identifier could be resolved to just add it as a property to that object rather than going back to the variable object in the execution context.
I doubt that is the reason. More likely it was to make it easier for
people who don't know the first thing about scopes, to write Javascript.
Making variables global removed the need for explaining scope.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:oe**********@hotpop.com... Yes, I suppose the decision could have gone either way. It probably seemed easier, having traversed the scope chain to the global object in order to determine whether the identifier could be resolved to just add it as a property to that object rather than going back to the variable object in the execution context.
I doubt that is the reason. More likely it was to make it easier for people who don't know the first thing about scopes, to write Javascript. Making variables global removed the need for explaining scope.
Yes, maybe. Though making undeclared variables become global doesn't
remove the need to explain scope, it just delays it and allows a lot of
bad habits to develop in the mean while.
Richard.
On 2 Nov 2003 07:02:43 -0800, Lee <RE**************@cox.net> wrote: re************@hotmail.com said:
I wasn't thinking of them being special as such, just I thought that if it wasn't an already declared variable then it would be created locally, not globally. That obvioulsy isn't the case.
I may have gotten confused with another loagnauge I was looking at a while back, but If I was I can't remember which language.
Oh the things that you really miss from working with a compiliable language, like syntax checking and scope checking etc.
Most compiled languages require you to declare all variables. The fact that Javascript *doesn't* is what got you into trouble. Scope and syntax checking aren't much use if a typo can result in the creation of a new variable.
that what I meant. having to declare everything up front usually stops
things like this from happening. PLus you can check syntax etc,
without having to actually run the code This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ron |
last post by:
have been stuck on this for several days now.
I am trying to create a reverse polish calculator and I'm stuck at an
intermediate stage.
This is what I know I have to do (just not sure how to do it...
|
by: GeorgeAtkins |
last post by:
VS 2003 using VB.NET
I'm writing a program that reads text entries from a one file and matches
them against values in an XML document. However, the code seems to just sit
in the loop and never get...
|
by: Mark Phillips |
last post by:
Hello,
I am having a problem in which a file can get stuck open when a thread
that is attempting to write to it gets aborted (ThreadAbortedException
occurs). The log file gets stuck open until...
|
by: Rene |
last post by:
I got stuck on an infinite loop on the "Paint" event of my custom control
and I was not able to "ctrl-break" my way out of it.
After trying everything, I had to ctrs-alt-delete to shut down the...
|
by: Mike D |
last post by:
Attached is my code. Which I know there is a better way of writing however
this is what I came up with and it works until the value is null or not = to
the <> value.
'Do While rdrSQL.Read()
...
|
by: steve marchant |
last post by:
trying to learn VB6. Simple counting loop which counts to 8 in 1 sec
intervals, then starts from 1 again and repeats.
Have two Command buttons on the form. Cmd1 starts the counting, and I need
to...
|
by: Tom C |
last post by:
We have a window named FormApplication which just happens to be our
mid parent window. When I open it in the designer, it is stuck in a
loop redisplaying a c1flexgrid. maxing out our cpu's. By...
|
by: hexusnexus |
last post by:
I wrote a simple algorithm and it keeps getting stuck in a loop. I
guess I'm just to tired to figure it out:
compcount=
suitrank=
trump=2
l,lt=0,0
while l<4:
while lt<4:
|
by: Cainnech |
last post by:
Hi guys,
I'm a bit stuck in an infinite loop. What I'm trying to do in the code below is to compare two arrays and if there's an item in array ALGEMEEN that is not in array ITEMS, I want to add it...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |