eval(function) doesn't work on IE

A

Andrew

I need to call a function inside another function, but I don't know the
function name.
I solved in this way:

function x (newFunc)
{
eval(newFunc+ "()");
}

Again this work for Firefox and other browser, but not in IE. Any idea
on how to solve this?

Thanks
Andrew
 
W

web.dev

Andrew said:
I need to call a function inside another function, but I don't know the
function name.
I solved in this way:

function x (newFunc)
{
eval(newFunc+ "()");
}

Again this work for Firefox and other browser, but not in IE. Any idea
on how to solve this?

Your use of eval is completely unnecessary. If you are passing a
reference to your function, you may invoke it directly. For example:

function x(fnc_ref)
{
fnc_ref();
}

function y()
{
//...etc.
}

x(y);
 
D

Dr J R Stockton

In comp.lang.javascript message
Tue said:
If you are passing a
reference to your function, you may invoke it directly. For example:

function x(fnc_ref)
{
fnc_ref();
}

And if you need the name of the current fnc_ref within function x,
Str = fnc_ref.toString().match(/function\s+(\w+)\W/)[1]
will do it for reasonably-named functions in at least some browsers.

For the general case, improve the RegExp.


Query : is there nowadays a respectable means XXXX of getting a
reference to a function given only its name ?

e.g. ISNAN == XXXX("isNaN")

Of course, XXXX being eval should do it; could that be deemed an
acceptable use, or is there a more specific alternative?

It's a good idea to read the newsgroup and its FAQ. See below.
 
R

Randy Webb

Dr J R Stockton said the following on 12/13/2006 1:10 PM:
In comp.lang.javascript message
Tue said:
If you are passing a
reference to your function, you may invoke it directly. For example:

function x(fnc_ref)
{
fnc_ref();
}

And if you need the name of the current fnc_ref within function x,
Str = fnc_ref.toString().match(/function\s+(\w+)\W/)[1]
will do it for reasonably-named functions in at least some browsers.

For the general case, improve the RegExp.


Query : is there nowadays a respectable means XXXX of getting a
reference to a function given only its name ?

e.g. ISNAN == XXXX("isNaN")

Of course, XXXX being eval should do it; could that be deemed an
acceptable use, or is there a more specific alternative?

If XXXX is a function name defined like any other, then window['XXXX']()
will call that function.
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top