简体   繁体   English

¿有人可以向我解释 JavaScript 中的回调吗? - 初学者

[英]¿Someone can explain to me Callback in JavaScript? - beginner

i im a beginner in this world and i feel like a im a little bit loset in thist.我是这个世界的初学者,我觉得自己有点迷路了。

i buy a course in udemy and this is the example:我在 udemy 买了一门课程,这就是例子:

function add( a, b, cb) {
    const r = a + b 
    cb(r)
}

function callback(result) {
    console.log('This is the result', result);
}

add(2, 3, callback)

This is the part i dont understand: cb(r) - ¿Why the "cb" are in that part of my code (add(a,b, cb )) and repeat later?这是我不明白的部分:cb(r) - 为什么“cb”在我的代码的那部分(添加(a,b, cb ))并稍后重复? i undestan we save the result of const r in Cb(r) but i dont know why is in both parts我想知道我们将 const r 的结果保存在 Cb(r) 中,但我不知道为什么在两个部分中

¿Why when i run this in my terminal it came callback function firts and add function?为什么当我在我的终端中运行它时,它首先回调 function 并添加 function?

if someone can i explain to me what are happen here step by step i goint to appreciate it如果有人可以向我解释这里发生了什么,我将不胜感激

i just whant to know how this works because its sound like is important on this path, it runs but i dont know why我只是想知道它是如何工作的,因为它听起来在这条路上很重要,它运行但我不知道为什么

The order in which you wrote function add and function callback doesn't matter.您编写function addfunction callback的顺序无关紧要。 They're just definitions, ie recipes on how to do something.它们只是定义,即关于如何做某事的食谱。 But unless they're called, nothing is actually done by just writing functions.但是除非调用它们,否则仅通过编写函数实际上什么也做不了。

By writing function add( a, b, cb) , you just say " I am writing a function named add, which is expected to receive 3 parameters as an input, and I name them a, b and cb. " But you don't know yet what their value is.通过写function add(a, b, cb) ,你只是说“我正在写一个名为 add 的 function,它应该接收 3 个参数作为输入,我将它们命名为 a、b 和 cb。 ”但你不知道还不知道它们的价值是多少。 You just name them this way in order to be able to do things with them.您只需以这种方式命名它们,以便能够对它们进行操作。 When you call cb(r) you have basically no idea what cb is, you just know it's a function to be completed later.当你调用cb(r)时,你基本上不知道 cb 是什么,你只知道它是一个稍后要完成的 function。

Basically your code is not going to be executed in the order in which you wrote it.基本上你的代码不会按照你写的顺序执行。

Javascript starts doing things when it reads add(2, 3, callback) . Javascript 在读取add(2, 3, callback)时开始执行操作。 At this point it understands that it needs to execute function add , to which you've given three parameters, two of which are number and the third one is a function. But it doesn't execute the function callback yet, it just knows its name.此时它明白它需要执行function add ,你给它三个参数,其中两个是数字,第三个是 function。但是它还没有执行function 回调,它只知道它姓名。 For now it's just going to search into your code where the function add is and execute this one first, with a=2, b=3, and cb=callback .现在,它只是搜索function add所在的代码,然后首先执行此代码,其中a=2、b=3 和 cb=callback

Once it found it, jumps to the beginning of the function add , creates a new constant r and puts a+b in it.一旦找到它,就会跳转到function add的开头,创建一个新常量 r并将a+b放入其中。 When it sees Cb(r) , it takes your third parameter that you've submitted to it, in this case the function callback , and calls it with parameter r .当它看到Cb(r)时,它采用您提交给它的第三个参数,在本例中为function 回调,并使用参数r调用它。 So again, it jumps to the beginning of function callback and executes it with parameter r .因此,它再次跳转到 function 回调的开头,并使用参数r执行它。

In this example, there is no need to use a callback function, that's just a basic exemple to illustrate the concept of callbacks.在此示例中,无需使用回调 function,这只是说明回调概念的基本示例。 But as said in the comments of your post, you may want to write a second callback function, such as:但是正如您帖子的评论中所说,您可能需要编写第二个回调 function,例如:

function anotherCallback(result) {
    console.log('This is ANOTHER result', result);
}

And then you could call然后你可以打电话

add(2, 3, callback)
add(4, 5, anotherCallback)

without having to re-write the function add.无需重新编写 function 添加。

I've tried to make it as detailed as possible, I hope this is clear but if not don't hesitate to ask further questions.我已尝试使其尽可能详细,我希望这是清楚的,但如果不清楚,请不要犹豫,提出更多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM