简体   繁体   English

回调函数的新增功能(将回调作为参数传递)(JavaScript)

[英]New to callback functions (passing a callback as parameter)(Javascript)

Newbie question here. 新手问题在这里。

I am trying to understand callback functions better. 我试图更好地理解回调函数。 Reading about them here, i though i grasped the idea. 在这里阅读有关它们的信息时,尽管我掌握了这个主意。

  function greeting (name, callback){ console.log(`Greetings ${name}`); callback(); }; function timeOfDay (time){ console.log(`How are you this fine ${time}?`); }; greeting ('Brad', timeOfDay('evening') ); 

Output 输出量

How are you this evening?
Greetings Brad
Uncaught TypeError: callback is not a function

Can someone please explain why the output is in this order? 有人可以解释为什么输出按此顺序吗? What does this error refer to and why does it appear even though the code has finished? 此错误指的是什么,即使代码已完成,为什么还会出现此错误?

I first did a simple callback function along the same structure and it worked fine. 我首先按照相同的结构做了一个简单的回调函数,并且效果很好。

Thanks in advance Brad 预先感谢布拉德

You were close, but when passing timeOfDay("evening") , you aren't actually passing that function as the callback. 您已经关闭了,但是当传递timeOfDay("evening") ,实际上并没有传递该函数作为回调。 That is a function invocation and it runs immediately and whatever the return value of that is, is what is passed to the greeting function invocation as the second argument. 那是一个函数调用,它立即运行,无论返回值是什么,它都是作为第二个参数传递给greeting函数调用的。 Since timeOfDay doesn't return anything, you are passing undefined to greeting . 由于timeOfDay不返回任何内容,因此您将undefined传递给greeting

The solution is to pass an actual function, not the result of invoking one (unless that result is, in fact, another function), to greeting , and one way we can do that is by wrapping the timeOfDay() function call in an anonymous function declaration, like this: 解决方案是将一个实际函数而不是调用一个函数的结果(除非结果实际上是另一个函数)传递给greeting ,而我们可以做到的一种方法是将timeOfDay()函数调用包装在匿名函数中函数声明,如下所示:

 function greeting (name, callback){ console.log(`Greetings ${name}`); callback(); }; function timeOfDay (time){ console.log(`How are you this fine ${time}?`); }; greeting ('Brad', function() { timeOfDay('evening') } ); 

Another technique is to use the Function.bind() method, which returns a copy of the function you've called it on, but the context that the function will execute in is configurable by you, based on the first argument you pass to .bind() . 另一种技术是使用Function.bind()方法,该方法返回您调用过的函数的副本,但是您可以根据传递给的第一个参数来配置函数将在其中执行的上下文.bind() This is a very useful technique, but requires a good knowledge and understanding of scope and context, which you can read more about in another answer of mine : 这是一种非常有用的技术,但是需要对范围和上下文有很好的了解和理解,您可以在我的另一个答案中了解更多:

 function greeting (name, callback){ console.log(`Greetings ${name}`); callback(); }; function timeOfDay (time){ console.log(`How are you this fine ${time}?`); }; greeting ('Brad', timeOfDay.bind(this,'evening') ); 

As said in the comments, when you do something like this: 如评论中所述,当您执行以下操作时:

greeting ('Brad', timeOfDay('evening') );

you will be calling the timeOfDay function instantaneously. 您将立即调用timeOfDay函数。

To avoid that you can use one of these options: 为避免这种情况,您可以使用以下选项之一:

  1. Wrap your function call in an anonymous function, like said in another answers. 如另一个答案中所述,将函数调用包装在匿名函数中。

  2. You can omit/remove the parenthesis, like this greeting('Brad', timeOfDay); 您可以省略/删除括号,例如: greeting('Brad', timeOfDay); (this avoid instant function call, but you'll lose your parameter "evening" and the error will persist). (这避免了立即调用函数,但是您将丢失参数"evening" ,并且错误将持续存在)。

  3. You can .bind() a context to the function, in the example below, I'm binding this as the context to the function, that way it's not going to call the function instantaneously. 您可以.bind()上下文的功能,在下面的例子中,我结合this作为背景的功能,这样它不会瞬间调用的函数。

take a look: 看一看:

 function greeting (name, callback){ console.log(`Greetings ${name}`); callback(); }; function timeOfDay (time){ console.log(`How are you this fine ${time}?`); }; greeting ('Brad', timeOfDay.bind(this, 'evening') ); 

Further reading: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind 进一步阅读: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_objects/Function/bind

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

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