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?
From stackoverflow
-
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 whatthis
will refer to, or your code will not work as expected if you are feeding a literal that referencesthis
inside it's function body.Andrew : hmm...seems to work fine, but firebug is complaining that `callback()` is not a function. Maybe a bug in firebug?meder : Are you sure it complains that `callback` isnt a function, or does it complain about `run`? Firebug says `run is not defined` for me ( Chrome console executes it perfectly ). The reason is because code evaluated in Firebug directly is not treated in the same manner as if the code was in a web page. If it's not complaining about `run` then check for typos.
0 comments:
Post a Comment