简体   繁体   English

作为参数传递的函数是否总是回调? JavaScript的

[英]Are functions passed as parameters always callbacks? JavaScript

If I have the code below, where I pass two functions as a parameters into the function sayHi , is this an example of a callback? 如果我有下面的代码,我将两个函数作为参数传递给函数sayHi ,这是回调的示例吗?

I notice there are two ways of running these 'parameter functions': either as below, we I call the functions where they are defined (as arguments), or alternatively where I call the parameter in the sayHi function. 我注意到有两种方法可以运行这些“参数函数”:如下所示,我们在定义它们的函数中调用它们(作为参数),或者在sayHi函数中调用参数。 Would this be the difference between a callback and an anonymous function? 这是回调和匿名函数之间的区别吗?

function sayHi(name, testForTrue) {
    if (testForTrue == true) {
        console.log(name);
    }
}

sayHi(function() {
    return 'Zach'
}(), function() {
    return true;
}());

Another way I could get the same result, is as below. 我可以得到相同结果的另一种方法如下。 In this case I am evaluating the functions at a different time? 在这种情况下,我要在其他时间评估功能? Is there any practical difference between the two? 两者之间有什么实际区别吗?

function sayHi(name, testForTrue) {
    if (testForTrue() == true) {
        console.log(name());
    }
}

sayHi(function() {
    return 'Zach'
}, function() {
    return true;
});

Yes, functions passed as parameters are always callbacks, even if the intention is that the function is called synchronously (cf Array.prototype.map ) rather than asynchronously (cf window.setTimeout ). 是的,作为参数传递的函数始终是回调,即使目的是同步调用该函数(参见Array.prototype.map )而不是异步调用(参见window.setTimeout )。

In your first code block you aren't of course actually passing functions. 当然,在第一个代码块中,您实际上并没有传递函数。 You have two immediately invoked function expressions , where the key part in this context is immediately invoked . 您有两个立即调用的函数表达式 ,其中此上下文中的关键部分被立即调用 The function expressions are called at the point they appear in the code and only the results of those expressions are passed to sayHi . 函数表达式在代码中出现的那一刻被调用,并且仅将这些表达式的结果传递给sayHi

In your first example you're not passing functions, but values; 在第一个示例中,您没有传递函数,而是传递值; in other words 换一种说法

(function(){ return 3; })()

is just the integer 3. 只是整数3。

It is a value obtained calling immediately a function, but this is irrelevant. 它是立即调用函数获得的值,但这无关紧要。

When you pass a callback it's the receiver that will call it (or pass it to some other function) and the code will be executed later, and not at the call site. 当您传递回调时,将由接收方对其进行调用(或将其传递给其他函数),并且代码将稍后执行,而不是在调用站点执行。

I guess that would depend on what your callback function is actually doing. 我想这将取决于您的回调函数的实际作用。

In your examples, all you're really doing is returning a value. 在您的示例中,您真正要做的就是返回一个值。 That's not really a "function", it's returning one fixed value every single time. 那实际上不是一个“函数”,它每次都返回一个固定值。

If your function is actually doing a process, or returning a varied result, then I would personally consider it a callback. 如果您的函数实际上正在执行一个过程,或者返回一个变化的结果,那么我个人将其视为回调。 (The name of it is self-explanatory, really). (它的名字确实是不言而喻的)。 Your script shouldn't rely on it, rather have it be a handler for the result of the function. 您的脚本不应依赖它,而应将其作为函数结果的处理程序。

For instance, something like this would be what I consider a callback function: 例如,这样的事情就是我认为的回调函数:

function doSomething(callback) {
  var userInput = prompt("hello, enter a number 1-10"),
      hasWon = false;

  if (userInput === "3") hasWon = true;

  callback(hasWon);
};

With this provided, we can call it like this: 有了这个,我们可以这样称呼它:

doSomething(function(hasWon){
  if (hasWon) alert("Congratz! You guessed my lucky number!")
});

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

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