JavaScript: How to pass an anonymous function as a function parameter?
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 someth开发者_如何学Pythoning 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.
精彩评论