Does this fit your definition of a Callback?

陌路散爱 提交于 2019-12-05 08:05:25

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"

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.

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.

yes, that's a callback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!