Javascript function as a parameter to another function?
Posted
by Alex
on Stack Overflow
See other posts from Stack Overflow
or by Alex
Published on 2010-05-13T04:22:30Z
Indexed on
2010/05/13
4:24 UTC
Read the original article
Hit count: 245
JavaScript
|functions-as-parameters
Hello there,
I'm learning lots of javascript these days, and one of the things I'm not quite understanding is passing functions as parameters to other functions. I get the concept of doing such things, but I myself can't come up with any situations where this would be ideal?
My question is:
When do you want to have your javascript functions take another function as a parameter? Why not just assign a variable to that functions return value and pass that variable to the function like so:
// Why not do this
var foo = doStuff(params);
callerFunction(foo);
//instead of this
callerFunction(doStuff);
I'm confused as to why I would ever choose to do things as in my second example.
Why would you do this? What are the use cases?
Thanks!!
© Stack Overflow or respective owner