By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

pass function into another function as parameter?

P: n/a
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.
Nov 21 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Nov 20, 3:57*pm, soni2926 <soni2...@yahoo.comwrote:
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
Thanks.
Nov 21 '08 #2

P: n/a
On Nov 20, 9:57*pm, soni2926 <soni2...@yahoo.comwrote:
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
Nov 21 '08 #3

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

P: n/a
On Nov 20, 5:57*pm, soni2926 <soni2...@yahoo.comwrote:
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" ) )
Nov 21 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.