473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using eval() to call functions - an unanticipated problem

Today I ran into a problem that had me completely baffled.
A function that I have just written, processes data in any one of 4
different data grids.
Within a for() loop I need to call another specific function depending
on the grid being accessed. To do this I construct the function name
from a variable and a literal string. No problem yet.

When I ran the web page that called the function, the for() loop kept
going into what appeared to be an infinite loop(?), but I wasn't
updating the counter variable - or so I thought.

Eventually, it dawned on me what was happening. The called funtions
also have a for() loop using the same counter variable (both "i").
While neither function should be aware of the other's variable as they
were both locally declared and therefore should have been out of
scope, the called function was updating the calling function's counter
variable.

This seems weird in the extreme, but it was happening with both
FireFox and Internet Explorer, so it seem to be a javascript "thing".

The problem disappeared of course after I changed the calling
function's counter variable.

Something to be aware of, folks!
Feb 2 '08 #1
3 4272
crater wrote on 02 feb 2008 in comp.lang.javascript:
Today I ran into a problem that had me completely baffled.
A function that I have just written, processes data in any one of 4
different data grids.
Within a for() loop I need to call another specific function depending
on the grid being accessed. To do this I construct the function name
from a variable and a literal string. No problem yet.

When I ran the web page that called the function, the for() loop kept
going into what appeared to be an infinite loop(?), but I wasn't
updating the counter variable - or so I thought.

Eventually, it dawned on me what was happening. The called funtions
also have a for() loop using the same counter variable (both "i").
While neither function should be aware of the other's variable as they
were both locally declared and therefore should have been out of
scope, the called function was updating the calling function's counter
variable.

This seems weird in the extreme, but it was happening with both
FireFox and Internet Explorer, so it seem to be a javascript "thing".

The problem disappeared of course after I changed the calling
function's counter variable.

Something to be aware of, folks!
I would think it to be a simple programming error on your part,
not an error of the interpreting programme.

Limiting scope of a variable is one of the nice things of javascript,
painfully absent in some other languages.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 2 '08 #2
crater wrote:
Today I ran into a problem that had me completely baffled.
Eventually, it dawned on me what was happening. The called funtions
also have a for() loop using the same counter variable (both "i").
While neither function should be aware of the other's variable as they
were both locally declared and therefore should have been out of
scope, the called function was updating the calling function's counter
variable.
Something to be aware of, folks!
As the others have said, it's more likely an error in your code. Here's
the most common error in for loops:

function somefunc()
{
for(i=0;i<something;i++)
{
dosomething();
}
}

Is that what your loop (basically) looked like ? If it does, then you're
missing the crucial var keyword that makes that i variable local to the
function. If you don't have it, you're declaring/sharing a global
variable. Here's how that look should look:

for(var i=0;i<something;i++)
{
dosomething();
}
Feb 2 '08 #3
Is that what your loop (basically) looked like ? If it does, then you're
missing the crucial var keyword that makes that i variable local to the
function. If you don't have it, you're declaring/sharing a global
variable. Here's how that look should look:

for(var i=0;i<something;i++)
{
* *dosomething();

}- Hide quoted text -

- Show quoted text -
Yes! You hit the nail on the head. I'd obviously developed tunnel
vision with this.
A nudge once in a while with a sharp elbow does wonders for clarity of
mind.
Many thanks.
Feb 4 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: knocte | last post by:
Hello. I have always thought that the eval() function was very flexible and useful. If I use it, I can define functions at runtime!! However, I have found a case where eval() does not work...
10
by: headware | last post by:
I know that you can call the method of one from from inside another form by doing something like this Forms("MyForm").MyFunction(12, 34) However, you have to know that MyForm has a function...
2
by: Kostis | last post by:
I have a form with 100 images (named from img1 to img100) and a Click event associated to each one. I want to write a function that calls these events. I have tried the following: Dim...
5
by: Alex | last post by:
I have a load of function names stored in a table which I want to use to decide which function to run. I can select the function name but I cannot work out how to call the function if I have the...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
8
by: santel_helvis | last post by:
Hi, Here is my code. function CallDisplay() { nodeobj=new mynode(); x="displayId("+nodeobj+")"; eval(x);
3
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do...
7
by: | last post by:
I have what's probably a simple page lifecycle question related to dynamically evaluating values that are placed by a repeater and dynmically placing user controls that use those values. I'm...
7
by: Darko | last post by:
Hello, I have this particular problem with eval() when using Microsoft Internet Explorer, when trying to define an event handler. This is the code: function BigObject() { this.items = new...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.