lincolnk provided a great answer. It can be made much more flexible using the arguments array:
function f1() {
var i;
// ...
for (i = 0; i < arguments.length; ++i)
{
if (typeof arguments[i] === 'function')
{
arguments[i]();
}
}
}
Like this, you can pass in as many callback functions as you want. They will be executed in the order passed in. No harm is done if the argument is not a callback.
Finally, you can preserve the context and arguments of f1 using apply().
Preserving the context could definitely be useful in many situations. I'm not sure about the arguments, but it is an option.
function f1() {
var i;
// ...
for (i = 0; i < arguments.length; ++i)
{
if (typeof arguments[i] === 'function')
{
// You can leave off 'arguments', but preserving 'this' will
// often be useful.
arguments[i].apply(this, arguments);
}
}
}
the above allows you to do things like:
f1("alert me",function() {alert(arguments[0]);});
// Output when the call back is called:
// "alert me"