There are 3 main possibilities to execute a function:
var callback = function(x, y) {
// "this" may be different depending how you call the function
alert(this);
};
- callback(argument_1, argument_2);
- callback.call(some_object, argument_1, argument_2);
- callback.apply(some_object, [argument_1, argument_2]);
The method you choose depends whether:
- You have the arguments stored in an Array or as distinct variables.
- You want to call that function in the context of some object. In this case, using the "this" keyword in that callback would reference the object passed as argument in call() or apply(). If you don't want to pass the object context, use null or undefined. In the latter case the global object would be used for "this".
Docs for Function.call,
Function.apply