回调函数的新增功能(将回调作为参数传递)(JavaScript) [英] New to callback functions (passing a callback as parameter)(Javascript)

查看:98
本文介绍了回调函数的新增功能(将回调作为参数传递)(JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是新手问题。



我试图更好地理解回调函数。在这里阅读有关它们的信息时,尽管我掌握了这个主意。



 函数问候(名称,回调){console.log(`Greetings $ {name}`);打回来(); }; function timeOfDay(time){console.log(`罚款$ {time}怎么样?)); };问候语('Brad',timeOfDay('evening'));  



输出

 今天晚上好吗? 
问候Brad
未捕获的TypeError:回调不是函数

有人可以吗解释为什么输出按此顺序?该错误指的是什么,即使代码已完成,为什么还会出现该错误?



我首先按照相同的结构做了一个简单的回调函数,并且工作正常。



预先感谢
Brad

解决方案

关闭,但是在传递 timeOfDay( evening)时,实际上并没有传递该函数作为回调。那是一个函数调用,它立即运行,无论返回值是多少,它都是作为第二个参数传递给 greeting 函数调用的内容。由于 timeOfDay 不返回任何内容,因此您将 undefined 传递给 greeting



解决方案是将一个实际函数而不是调用一个函数的结果(除非该结果实际上是另一个函数)传递给打招呼,而做到这一点的一种方法是将 timeOfDay()函数调用包装在一个匿名函数声明中,如下所示:



 函数问候语(名称,回调){console.log(`问候语$ {name}`); callback();}; function timeOfDay(time){console.log(`罚款$ {time}?``);};};问候('Brad',function(){timeOfDay('evening')}) ;  



另一种方法是使用 Function.bind() 方法,该方法返回您调用过的函数的副本,但是您可以根据传递的第一个参数来配置函数将在其中执行的上下文到 .bind()。这是一种非常有用的技术,但是需要对范围和上下文有很好的了解和理解,您可以阅读有关 在我的另一个答案中



< pre class = snippet-code-js lang-js prettyprint-override> 函数问候语(名称,回调){console.log(`Greetings $ {name}`);; callback();}; function timeOfDay(time){console.log(`罚款$ {time}?``);};};问候('Brad',timeOfDay.bind(this,'evening'));


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. 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. Since timeOfDay doesn't return anything, you are passing undefined to 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:

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(). 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') );

这篇关于回调函数的新增功能(将回调作为参数传递)(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆