Callbacks, let me try and explain it in a way that you can understand.
- Firstly you need to understand how a function can be assigned to a variable without being executed.
This allows you to pass around functions and only executed them when you want, heres an example how I can pass a function to a variable and not executing it:
a = function(){}
This is a small example and most common, you would call the function like a()
adding the ()
tells the function to execute.
- Now you should understand that you can pass these variables into another function, yet not calling them
the below example shows you how you can pass a function into the above function a
whilst executing it.
b = function(){}
b(a);
- the next step is to be able to create anonymous functions, these will allow you to assign a function to a variable whilst passing into another function
an anonymous function is not assigned at the point of writing it but is assigned when its parsed within the outer functions scope.
the above my be a little tricky to understand but hopefully the example will clear it up.
a(
function(){}
);
The above is the same as the b(a)
example apart from a
is now an anonymous function, and is only assigned to the variable within b
, i.e the outer function.
jQuer'ies API relies heavily on anonymous functions as there easy to use and create separation from the jQuery Core and the users code, the user does not need to heavily rely on the main code of jQuery but pass a callback to allow jQuery to connect to your code rather.
an example of a function that uses similar means of passing data in and out of functions are like so:
JoinUser = function(Firstname,Lastname,callback)
{
Fullname = Firstname + " " + Lastname;
callback(Fullname);
}
JoinUser("Robert","Pitt",function(Fullname)
{
alert(Fullname); //Robert Pitt
})
the above example only shows you roughly how the style of jQuery works, but not the most important benefit of callbacks and that's Ajax.
Ajax can halt a javascript script until its data has been received, for example:
Result = RequestPage("Contacts");
when Ajax sees this line it has to wait for RequestPage
to return the result because the coding further down is expected to require this value, so we have to wat for the data to be returned from the server before JavaScript continues, this can be several seconds and cause the UI To act abnormally.
The way we get around this is to send a callback to the function and tell it to call when its done as I want to work on the rest of the application.
RequestPage("Contacts",function(){});
so now the JavaScript continues down the page performing the task it needs, and within the RequestPage
function it would be layed out like so:
function RequestPage(page,callback)
{
//..
request.oncomplete = callback;
//..
}
This way the rest of your application has done other task and when the result is ready you can change the UI froim the result.
This is also why we use loaders to replicate a working state.
I hope you begin to understand how jQuery takes advantage of these factors of javaScript and it will help you to develop your application.