Using eval() to call functions - an unanticipated problem

C

crater

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!
 
E

Evertjan.

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.
 
J

Joost Diepenmaat

crater said:
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.

Just in case, you don't need to do that. You can pass functions around
just like any other object:

function MyFunc() {}
function MyOtherFunc() {}

var funcByName = {
a: MyFunc,
b: MyOtherFunc,
c: function() { ... }
}

function callme(name) {
funcByName[name].call();
}

callme("a");
callme("b");
callme("c");

For instance.

Much safer, and also probably an order of magnitude faster.
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".

Depending on how you do that exactly, it may be the case that
function.toString() is getting called somewhere, which would "inline"
your function code in the eval statement, instead of calling the
function.
Something to be aware of, folks!

Sure, you should always be careful with eval(), and if possible, avoid
it altogether.

Joost.
 
S

Stevo

crater said:
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();
}
 
C

crater

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top