开发者

JavaScript: How to pass an anonymous function as a function parameter?

开发者 https://www.devze.com 2023-01-17 16:18 出处:网络
I would like to write a function that accepts an anonymous function as a parameter. For example: run(\'param1\', function(){

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消