pass function into another function as parameter?

S

soni2926

hi,
is it possible to pass a function into another function as a
parameter? Say i have these:

function SaveMe(text)
{...}

function SaveMeNow(text)
{...}

function WhichToSave(x, y, z)
{...}

a button will call WhichToSave, that function will perform some logic
and then call one of the Save methods, can i pass that method in as
say parameter z and have it just call the function right away, passing
one of the SaveMe methods a parameter which is determined by the
WhichToSave method?

Thanks.
 
D

dhtml

hi,
is it possible to pass a function into another function as a
parameter?  Say i have these:

function SaveMe(text)
{...}

function SaveMeNow(text)
{...}

function WhichToSave(x, y, z)
{...}

a button will call WhichToSave,

How does a button call something? Is WhichToSave a callback from an
event handler attached to a button, as in:-

button.onclick = WhichToSave;

?
that function will perform some logic
and then call one of the Save methods, can i pass that method in as
say parameter z and have it just call the function right away, passing
one of the SaveMe methods a parameter which is determined by the
WhichToSave method?

Functions can be passed.

function invoker(f) {
f();
};

function a() {
document.title = "a";
}

invoker(a);

Garrett
 
G

Gabriel Gilini

hi,
is it possible to pass a function into another function as a
parameter?
Yes.

function foo(){
alert('foo');
}

function bar(fn){
fn();
}

bar(foo); // alerts 'foo'
 
W

William James

hi,
is it possible to pass a function into another function as a
parameter?

function reverse_str( s )
{ return s.split("").reverse().join("")
}

function upcase( s )
{ return s.toUpperCase()
}

funcs = [reverse_str, upcase]
for (var i = 0; i<funcs.length; i++)
document.write( "<p>" + funcs( "was i able" ) )
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top