By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 759 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

Using eval() to call functions - an unanticipated problem

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.