I would like to write a function that accepts an anonymous function as a parameter. For example:
run('param1', function(){
alert('execute this');
});
function run(param1, callback) {
//now execute the callback parameter as a function
}
How can I achieve something like this?
callback()
would invoke it.
If you need to supply a context, do callback.apply(this, arguments)
. When you use .apply
be aware of the current execution context, basically know what this
will refer to, or your code will not work as expected if you are feeding a literal that references this
inside it's function body.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With