signature with a callback

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

signature with a callback

Post by Live24x7 »

Hi
I am trying to figure out the use of callback functions in asynchronous requests in AJAX.

I come across terms like:
"signature with a callback" or "delegate"

Would really appreciate if some one could explain what signature means in this context.

Code: Select all


function mySandwich(param1, param2, callback) {  
    alert('Started eating my sandwich.\n\nIt has: ' + param1 + ', ' + param2);  
  
    $('#sandwich').animate({  
                opacity: 0  
    }, 5000, function() {  
        // Animation complete.  
    });  
  
    if (callback && typeof(callback) === "function") {  
        callback(param1, param2);  
    }  
}  

mySandwich('ham', 'cheese', function(callbackParam1, callbackParam2) {  
    alert('Finished eating my ' + callbackParam1 + ' & ' + callbackParam2 + ' sandwich.');  
}); 

thanks a lot
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: signature with a callback

Post by requinix »

In any language a signature of a function is the combination of its return value and the parameter types and count. The signature may only technically include, for example, the parameter count if the language doesn't support return value types and parameter types (like Javascript), but as a developer you're still responsible for accepting the right types of arguments and returning the right type of value.

So the signature in your example is

Code: Select all

function(callbackParam1, callbackParam2) {
and used by

Code: Select all

callback(param1, param2);
Specifically, the function (named "callback") should take two (or more) arguments; its return value isn't used so it doesn't matter.
Post Reply