I\'ve got a JavaScript application that uses a lot of callbacks. A typical function will take a callback, and wrap it with another callback.
Namespace.foo = func
A good example is when implementing a function that needs a callback. When writing OO code, you want to allow the caller to specify the context that a callback will be called.
function validateFormAjax(form, callback, context) {
// Using jQuery for simplicity
$.ajax({
url: '/validateForm.php',
data: getFormData(form),
success: function(data) {
callback.call(context, data);
}
});
}
Note that my example could just be implemented by passing the context parameter to the $.ajax
call, but that wouldn't show you much about using call