简体   繁体   English

将 Javascript Function 作为参数传递 - 这个逻辑如何工作?

[英]Passing a Javascript Function as Parameter - How Does This Logic Work?

I don't understand how the responseHandler function below is receiving a response from an API call, after a payment form submission.我不明白在提交付款表单后,下面的 responseHandler function 如何接收来自 API 调用的响应。 Here is a brief summary of the code:以下是代码的简要摘要:

var PaymentThing = function(e) {
    this["clientCallback"] = e;

    return {
        createToken: function() {
            // HTTP GET URL is generated and then an HTML script element is appended to the page head
            var a = "https://example.com/securitytoken?creditcard=xxxx?callback=PaymentThing.callback";
            var f = document.createElement("script");
            f.src = a;
            document.getElementsByTagName("head")[0].appendChild(f)
        },
        callback: function(e) {
            this["clientCallback"](e.status, e.results)
        }
    }
}


var responseHandler = function(status, response) {
    // How does this even work?
    console.log(response, status);
}


// Form Submission
jQuery('#checkout_payment_form').submit(function() {
    PaymentThing.createToken(responseHandler);
}

So my question is, how does the responseHandler function receive the response from the HTTP GET call?所以我的问题是,responseHandler function 如何接收来自 HTTP GET 调用的响应? It looks to me like the HTTP call isn't even being made.在我看来,HTTP 调用甚至没有进行。 I only see a URL being added to an HTML script element (with the createToken function), with the URL never even being called/executed (but somehow it is?).我只看到 URL 被添加到 HTML 脚本元素(使用 createToken 函数),URL 甚至从未被调用/执行(但不知何故它从未被调用/执行?)。 Also, how are the (status, response) parameters passed into the responseHandler function when I don't see them being passed into a responseHandler function call?另外,当我没有看到它们被传递到 responseHandler function 调用时,(状态、响应)参数是如何传递到 responseHandler function 的?

I don't understand the logic or order of operations here, can anyone please explain what's going on?我不明白这里的操作逻辑或顺序,谁能解释发生了什么?

EDIT: Ok I think I see now how the responseHandler function is receiving the status and response parameters.编辑:好的,我想我现在看到 responseHandler function 如何接收状态和响应参数。 I edited the code above to show there's a callback function in the generated HTTP url.我编辑了上面的代码以显示在生成的 HTTP url 中有一个回调 function。 But still, how is that URL being called?但是,如何调用 URL ? It still looks to me like it's inert and being stuck into the HTML of the page and not activating.在我看来,它仍然是惰性的并且被卡在页面的 HTML 中并且没有激活。

I think if you run the responseHandler function and store the result in a variable that can be called as a parameter in the PaymentThing.createToken() it will take it in consideration.我认为如果您运行responseHandler function 并将结果存储在一个变量中,该变量可以作为PaymentThing.createToken()中的参数调用,它会考虑到它。

None of the code there calls responseHandler .那里的代码都没有调用responseHandler There is no point in passing it to the createToken method as it completely ignores all the arguments.将它传递给createToken方法没有任何意义,因为它完全忽略了所有 arguments。

It is possible that the script which is added to the page attempts to call a global function named responseHandler , but there's no way to tell without inspecting the script.添加到页面的脚本可能会尝试调用名为responseHandler的全局 function ,但是如果不检查脚本就无法判断。

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

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