最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does this fit your definition of a Callback? - Stack Overflow

programmeradmin3浏览0评论

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 ponentB, 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 ponent like so...

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

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

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 ponentB, 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 ponent like so...

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

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

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 5, 2009 at 16:50 leeand00leeand00 26.4k45 gold badges155 silver badges305 bronze badges 4
  • what happens when you run this? – cbrulak Commented Mar 5, 2009 at 16:52
  • When the user clicks on the Component, after this code is run, a dialog "I was clicked" – leeand00 Commented Mar 5, 2009 at 16:58
  • Note that you can also use lambdas aka anonymous functions giving a bit less code, like this: ponentB.setMouseDownCallback( function() { alert("I was clicked!"); }); – Svante Svenson Commented Mar 5, 2009 at 17:05
  • Ah I wondered what a Lambda function was...okay clears that up! Thanks! – leeand00 Commented Mar 5, 2009 at 17:22
Add a ment  | 

5 Answers 5

Reset to default 3

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

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.

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; 
    ponentB.setMouseDownCallback(myCallback);
}

Would work.

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

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

is valid as well.

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

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 ponentB, in order for ponentB to call it later.

yes, that's a callback.

发布评论

评论列表(0)

  1. 暂无评论