简体   繁体   English

需要澄清回调在JavaScript中的工作方式

[英]Need clarification on how callbacks work in javascript

I have hard time understanding how this callback is called: 我很难理解如何调用此回调:

$(function () {
        function getData(callback) {
            var now = Date.now();
            var hour = 60 * 60 * 1000;
            var temperatures = [];
            for (var k = 24; k > 0; --k) {
                temperatures.push([now - k * hour,
                    Math.random() * 2 + 10 * Math.pow((k - 12) / 12, 2)
                ]);

                }
            callback ({data: temperatures });
        }

        getData(function (data) {
            $.plot("#chart", new Array(data));
        });
    });

I am well aware what is happening in the loop just need clarification with the callback. 我很清楚循环中正在发生什么,只需要对回调进行说明即可。 So, the callback is just a another function and in it we are creating a method? 那么,回调只是另一个函数,我们在其中创建一个方法吗? And assigning a tempretures array to it? 并为其分配一个tempretures数组? This part really confuses me. 这部分确实让我感到困惑。 If "data" in the callback is a method why I cant rename it to anything else? 如果回调中的“数据”是一种方法,为什么我不能将其重命名为其他名称? While I can rename to anything I want the "data" argument when I am calling getData function. 当我可以重命名为任何东西时,我在调用getData函数时都需要“ data”参数。

Can somebody provide a more understandable version of this callback and tempretures array relationship? 有人可以提供这种回调和tempretures数组关系的更易理解的版本吗? Thanks. 谢谢。

You are creating an object which has one property called data the value of which is an array (your temperature). 您正在创建一个对象,该对象具有一个称为data的属性,其值是一个数组(您的温度)。 This object is passed as the first parameter of callback. 该对象作为回调的第一个参数传递。

getData(function (data) {
  $.plot("#chart", new Array(data));
});

Here the object is being passed in as an argument called data . 在这里,对象作为名为data的参数传入。 As written, I don't think this will work since what you need to do is pass in the array to new Array() and not the object. 如所写,我认为这行不通,因为您需要做的是将数组传递给new Array()而不是对象。 So do this: 这样做:

getData(function (data) {
  $.plot("#chart", new Array(data.data));
});

It's a bit easier to understand if you rename the object property: 如果重命名对象属性,则更容易理解:

callback ({temperatures: temperatures});

getData(function (data) {
  $.plot("#chart", new Array(data.temperatures));
});

A callback is a way to say, "When you're done with what you're doing, call this method." 回调是一种表达方式,“当您完成正在执行的操作时,请调用此方法。”

JavaScript is single threaded, but it is asynchronous, so you may have some calls that finish before others. JavaScript是单线程的,但它是异步的,因此您可能需要先完成一些调用才能完成。 In these cases your code runs out of order. 在这些情况下,您的代码将无法正常运行。 Callbacks are a way to ensure that a method calls another method when it's finished and actions finish as you expect. 回调是一种确保方法完成时调用另一个方法并且操作按预期完成的方法。

In the case of your code, I'll annotate it as I go through: 对于您的代码,我将在进行注释时对其进行注释:

$(function () {
    function getData(callback) { //callback is a parameter, in our case a function to     be called later

        var now = Date.now();
        var hour = 60 * 60 * 1000;
        var temperatures = [];
        for (var k = 24; k > 0; --k) {
            temperatures.push([now - k * hour,
                Math.random() * 2 + 10 * Math.pow((k - 12) / 12, 2)
            ]);

            }
        callback ({data: temperatures }); //once you've calculated temperature, call the callback method with an object that has a property called 'data' and a value that is an array.
    }

    getData(function (data) {
        $.plot("#chart", new Array(data));
    });
});

So, the callback is just a another function and in it we are creating a method? 那么,回调只是另一个函数,我们在其中创建一个方法吗? And assigning a tempretures array to it? 并为其分配一个tempretures数组?

Kinda, the callback is a function, but you're not creating a method. 有点,回调是一个函数,但是您没有创建方法。 You're calling a method and assigning the array to it. 您正在调用一个方法并为其分配数组。

This part really confuses me. 这部分确实让我感到困惑。 If "data" in the callback is a method why I cant rename it to anything else? 如果回调中的“数据”是一种方法,为什么我不能将其重命名为其他名称?

Wrong..."data" is not a method, it's a single element object array. 错误的...“数据”不是方法,它是单个元素对象数组。 And you can name it whatever you want, as long as you change the name in the callback method...for example: 您可以随意命名,只要您在回调方法中更改名称即可。例如:

getData(function (newData) {
        $.plot("#chart", new Array(newData));
    });

While I can rename to anything I want the "data" argument when I am calling getData function. 当我可以重命名为任何东西时,我在调用getData函数时都需要“ data”参数。

You care calling getData because the for loop is in that function. 您需要调用getData因为该函数中包含for循环。 You are passing the callback method as a parameter in that function to handle the data once it's calculated. 您将在该函数中将callback方法作为参数传递,以在数据计算后处理该数据。

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

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