views:

479

answers:

5

Definition of Callback:

A Function that is set as a property within a Component. And is usually called when some event occurs on the Component.

For Example:

If you wish to display a dialog which reads "I was clicked" when the user clicks on the Component componentB, you would write a method stored as a variable which does this:

var mouseDownCallbackFunction = function() {
 alert("I was clicked!");
};

Next, you would set this function inside the component like so...

// Set the Component to display the dialog when the 
// user presses the mouse down on it.
componentB.setMouseDownCallback(mouseDownCallbackFunction);

And this would cause mouseDownCallbackFunction to display "I was clicked" in an alert box when the component was clicked.

A: 

yes, that's a callback.

Andrew Clark
+3  A: 

Yes, this is describing the exact definition of a callback...

John Rasch
+2  A: 

Yes, a callback is a function that's defined at a higher level than it is called. Your client code creates the function, then passes it as a parameter to componentB, in order for componentB to call it later.

Bill the Lizard
+2  A: 

In C, that would be a valid callback. However I'm not so familar with JavaScript to say if it is or not because I'm not sure how variables are treated with respect to their memory locations.

In C/C++ you could declare a void pointer:

void aFunction()
{
     do stuff
}

int main()
{
    void* myCallback = &aFunction; 
    componentB.setMouseDownCallback(myCallback);
}

Would work.

However, despite my lack of JavaScript knowledge, I do know that

componentB.setMouseDownCallback(function() {
        alert("I was clicked!");
        };
);

is valid as well.

EDIT added a not to the second sentence: "I'm not so familar"

cbrulak
Thanks! I work in a C++ shop, and I'm their web guy...this will help immensely in arguing my case! Thank you starko!
leeand00
right on, glad I could help :)
cbrulak
+2  A: 

In JavaScript, technically, that's a closure, since it can bind to any variables in scope which are referenced.

But closures are just a better form of callback, so yes that's a callback. A callback in C is more primitive, providing only a pointer reference to a typed function, without binding to any context.

Software Monkey
Hmm...then I wonder how a closure is different from a callback?
leeand00
...asking question here: http://stackoverflow.com/questions/615907/how-is-a-closure-different-from-a-callback
leeand00
The other question is well answered, and it's what I meant in my first statement in reference to a closure binding variables, vs. a C callback being just a function point.
Software Monkey