如何解释回调在简体中文?它们如何区别于从另一个函数调用一个函数? [英] How to explain callbacks in plain english? How are they different from calling one function from another function?

查看:137
本文介绍了如何解释回调在简体中文?它们如何区别于从另一个函数调用一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解释简单的英语回调?它们如何区别于从调用函数获取一些上下文的另一个函数调用一个函数?他们的权力如何解释给新手程序员?

How to explain callbacks in plain English? How are they different from calling one function from another function taking some context from the calling function? How can their power be explained to a novice programmer?

推荐答案

通常,应用程序需要根据其上下文/状态执行不同的功能。为此,我们使用一个变量,我们将存储有关要调用的函数的信息。根据它的需要,应用程序将设置此变量与要调用的函数的信息,并将使用相同的变量调用函数。

Often an application needs to execute different functions based upon its context/state. For this, we use a variable where we would store the information about the function to be called. ‪According to its need the application will set this variable with the information about function to be called and will call the function using the same variable.

在javascript中,示例是下面。这里我们使用方法参数作为一个变量,我们存储有关函数的信息。

In javascript, the example is below. Here we use method argument as a variable where we store information about function.

function processArray(arr, callback) {
    var resultArr = new Array(); 
    for (var i = arr.length-1; i >= 0; i--)
        resultArr[i] = callback(arr[i]);
    return resultArr;
}

var arr = [1, 2, 3, 4];
var arrReturned = processArray(arr, function(arg) {return arg * -1;});
// arrReturned would be [-1, -2, -3, -4]

这篇关于如何解释回调在简体中文?它们如何区别于从另一个函数调用一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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