Connecting Tech Pros Worldwide Forums | Help | Site Map

pass function into another function as parameter?

soni2926
Guest
 
Posts: n/a
#1: Nov 21 '08
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.

dhtml
Guest
 
Posts: n/a
#2: Nov 21 '08

re: pass function into another function as parameter?


On Nov 20, 3:57*pm, soni2926 <soni2...@yahoo.comwrote:
Quote:
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;

?
Quote:
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
Quote:
Thanks.
Gabriel Gilini
Guest
 
Posts: n/a
#3: Nov 21 '08

re: pass function into another function as parameter?


On Nov 20, 9:57*pm, soni2926 <soni2...@yahoo.comwrote:
Quote:
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'

--
Gabriel Gilini
soni2926
Guest
 
Posts: n/a
#4: Nov 21 '08

re: pass function into another function as parameter?


On Nov 20, 7:13*pm, Gabriel Gilini <gabr...@usosim.com.brwrote:
Quote:
On Nov 20, 9:57*pm,soni2926<soni2...@yahoo.comwrote:hi,
Quote:
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'
>
--
Gabriel Gilini
thank you!
William James
Guest
 
Posts: n/a
#5: Nov 21 '08

re: pass function into another function as parameter?


On Nov 20, 5:57*pm, soni2926 <soni2...@yahoo.comwrote:
Quote:
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[i]( "was i able" ) )
Closed Thread