这符合您对回调的定义吗?

发布于 2024-07-14 09:46:44 字数 583 浏览 11 评论 0原文

回调的定义:

设置为组件内属性的函数。 通常在组件上发生某些事件时调用。

例如:

如果您希望在用户单击组件 componentB 时显示一个显示“I was clicked”的对话框,您可以编写一个存储为变量的方法,该变量这样做:

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

下一步,您可以像这样在组件内设置此函数...

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

这将导致 mouseDownCallbackFunction 显示“我被点击了” 单击该组件时在警报框中显示。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

指尖微凉心微凉 2024-07-21 09:46:44

是的,这描述了回调的确切定义......

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

失眠症患者 2024-07-21 09:46:44

在 JavaScript 中,从技术上讲,这是一个闭包,因为它可以绑定到引用范围内的任何变量。

但闭包只是一种更好的回调形式,所以是的,这就是回调。 C 中的回调更为原始,仅提供对类型化函数的指针引用,而不绑定到任何上下文。

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.

记忆消瘦 2024-07-21 09:46:44

在 C 中,这将是一个有效的回调。 然而,我对 JavaScript 不太熟悉,无法判断是否是这样,因为我不确定如何根据变量的内存位置来处理变量。

在 C/C++ 中,您可以声明一个 void 指针:

void aFunction()
{
     do stuff
}

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

可以。

然而,尽管我缺乏 JavaScript 知识,但我确实知道这

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

也是有效的。

编辑在第二句话中添加了一个not:“我不太熟悉”

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"

溺深海 2024-07-21 09:46:44

是的,回调是一个定义在比调用更高级别的函数。 您的客户端代码创建该函数,然后将其作为参数传递给 componentB,以便 componentB 稍后调用它。

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.

无敌元气妹 2024-07-21 09:46:44

是的,这是一个回调。

yes, that's a callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文