Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass JScript function by ref into another function? 1

Status
Not open for further replies.

gatwick2002

Programmer
Jun 24, 2004
8
US
IE 6.0

I need to write something like

function a()
{
alert("QQQ");
}

function b(fn)
{
fn();
}

function c()
{
b(a);
}

to see "QQQ" msgbox. How can I do it? THe code I posted doesnt do it.
 

>> THe code I posted doesnt do it.

Are you ever calling "c();"? The code you've given works 100% for me.

Dan
 
Like Dan said, the code you posted works fine. So I'm guessing you want the value of a string to be interpreted as a function? If so, you could use eval():

function a()
{
alert("QQQ");
}

function b(fn)
{
eval(fn);
}

function c()
{
b("a()");
}



Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Actually, I take that back. eval() sucks. Don't ever use it if you can help it. Try this instead.

function a()
{
alert("QQQ");
}

function b(fn)
{
window[fn]();
}

function c()
{
b("a");
}

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 

Adam,

It's good to see someone else using the "window[fn]();" syntax... I started using it about 4 months ago as an alternative to eval... It's great, isn't it?

I think I've only ever found one example of a command I had to use eval for now... Unfortunately, I cannot remember it - maybe if I find it, I'll post a question here for alternatives ;o)

Dan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top