Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在回调对象中调用外部方法?_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript 如何在回调对象中调用外部方法?

Javascript 如何在回调对象中调用外部方法?,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我目前正在尝试为客户实现一种通过公司网站在线支付的方式,但在调用回调对象之外的其他方法时遇到了问题。以下是我试图执行的代码片段,其中有一些问题: OpenLightBox(txn_type, invoices, amount, customer){ $.get('/token_req', { amount: amount, invoice: `#9999999`,

我目前正在尝试为客户实现一种通过公司网站在线支付的方式,但在调用
回调
对象之外的其他方法时遇到了问题。以下是我试图执行的代码片段,其中有一些问题:

OpenLightBox(txn_type, invoices, amount, customer){

        $.get('/token_req', 
            {
                amount: amount, 
                invoice: `#9999999`, 
                type: txn_type == "cc" ? "ccsale" : "ecspurchase",
                email: customer.Email,

            }, (response) => {
        
            let token = response;

            let paymentFields = {
                ssl_txn_auth_token: token
            }

            let callback = {
                onError: (error) => {
                    console.error(error);   // api/payment_error
                },
                onDeclined: (response) => {
                    console.error(response.ssl_result_message); // api/payment_decline
                },
                onApproval: (response) => {
                    this.PayInvoices(response, invoices); // error this.PayInvoices is not defined
                }
            };
            
            PayWithConverge.open(paymentFields, callback);

            
        })
    }

    PayInvoices(response, invoices){
        console.log({invoices});
        console.log({response});
    }

    FormatDate2(dateIn){
       let [month,day,year] = dateIn.split('/');
       return `${year}-${month}-${day}`;
    }
PayWithConverge
是一种方法,它来自我的
index.html
文件中的异步脚本

当我有一个成功的交易,并且当我尝试调用我的
PayInvoices
方法时,就会出现问题。 我试着使用
.bind(这个)关于我能想到的一切,但仍然遇到相同的问题

我想知道是否可以调用这个回调对象之外的方法,如果可以的话,在我这里有代码的情况下,我该怎么做?谢谢

编辑

OpenLightBox
方法在我的代码中被调用如下:

<div className="ad-size-container fade-in">
                    <div className="page-header">
                        <h1>Pay Invoice</h1>
                    </div>
                    <Invoice invoices={this.state.Invoices} customer={this.state.Customer} getToken={this.OpenLightBox}></Invoice>
                </div>


付款发票

这是因为
指的是
get
函数中的对象

将它移到外面,在
$上方。获取

OpenLightBox(txn_type, invoices, amount, customer){

        let callback = {
            onError: (error) => {
                console.error(error); 
            },
            onDeclined: (response) => {
                console.error(response.ssl_result_message); 
            },
            onApproval: (response) => {
                this.PayInvoices(response, invoices); 
            }
        };
// .... rest of your code
因为箭头函数具有词法
绑定。
您也可以将其保留在原来的位置,但使用常规功能,即:

    let callback = {
        onApproval: function(response) {
            this.PayInvoices(response, invoices); 
        }
        // ... rest of code
    };

请告诉我们如何调用
OpenLightBox
(因为该调用决定了箭头函数中
this
的值)。这些函数都是类的一部分还是对象的一部分?@RossAllen这些函数都是React类的一部分。@Bergi我已经更新了我的问题,以显示如何调用它。@Michael谢谢。这是您需要使用
bind
或箭头函数的地方,即
/
this.OpenLightBox(…args)}>
。(或者将
bind
放在类的构造函数中)我认为使用正则函数与预期相反。OP使用箭头函数,目的是
应参考其自己的
付款发票
方法。按照您提出的方式,
this
将不再引用他们的类。此外,“get函数内的对象”不清楚。。。当然,传递给
$.get
的回调没有定义另一个
this
,因为这个回调是一个箭头函数……好吧,我明白了,第二种方法不起作用,但第一种方法应该起作用(将它移到
$.get
?如果它在
$.get
回调内部不工作,它也不会在它外部工作,因为
this
的值完全相同。关键是Bergi在注释中写的内容。。。