如何将自定义属性添加到函数回调 Javascript
How to add custom attributes to function callback Javascript
我有这个代码
doPurchase = function(amount) {
transactionCompleteCallbackImpl.bind(this, amount);
iclient.initiatePurchase({
amount: amount,
cashout: '0',
integratedReceipt: true
}, {
statusMessageCallback: statusMessageCallbackImpl,
questionCallback: questionCallbackImpl,
receiptCallback: receiptCallbackImpl,
transactionCompleteCallback: transactionCompleteCallbackImpl
});
};
我无法控制 iclient 代码,因为它是从外部源加载的,但是我希望添加购买金额(莫名其妙地)未包含在 transactionCompleteCallback 返回的数据中:
transactionCompleteCallbackImpl = function(response, transactionAmount) {
console.log(transactionAmount);
return console.log(response);
};
这首先记录 transactionAmount 的未定义,然后记录原始响应数据
如您所见,我已经尝试过 bind,我已经阅读过它可以以这种方式使用,但我认为有问题,因为我仍然无法访问我的回调函数中的数据。
任何帮助将不胜感激
Function.prototype.bind()
returns 具有正确上下文(this
值)和提供的参数的 new 函数。
所以您可以将 .bind()
行更改为:
transactionCompleteCallback = transactionCompleteCallback.bind(this, amount);
这样您就可以用绑定到 this
的正确值并将 amount
值作为第一个参数的函数替换 transactionCompleteCallback
函数。
bind() 将创建一个新方法但不会修改该方法,因此您的第一行应该是
transactionCompleteCallbackImpl = transactionCompleteCallbackImpl.bind(this, amount);
试试吧!
我有这个代码
doPurchase = function(amount) {
transactionCompleteCallbackImpl.bind(this, amount);
iclient.initiatePurchase({
amount: amount,
cashout: '0',
integratedReceipt: true
}, {
statusMessageCallback: statusMessageCallbackImpl,
questionCallback: questionCallbackImpl,
receiptCallback: receiptCallbackImpl,
transactionCompleteCallback: transactionCompleteCallbackImpl
});
};
我无法控制 iclient 代码,因为它是从外部源加载的,但是我希望添加购买金额(莫名其妙地)未包含在 transactionCompleteCallback 返回的数据中:
transactionCompleteCallbackImpl = function(response, transactionAmount) {
console.log(transactionAmount);
return console.log(response);
};
这首先记录 transactionAmount 的未定义,然后记录原始响应数据
如您所见,我已经尝试过 bind,我已经阅读过它可以以这种方式使用,但我认为有问题,因为我仍然无法访问我的回调函数中的数据。 任何帮助将不胜感激
Function.prototype.bind()
returns 具有正确上下文(this
值)和提供的参数的 new 函数。
所以您可以将 .bind()
行更改为:
transactionCompleteCallback = transactionCompleteCallback.bind(this, amount);
这样您就可以用绑定到 this
的正确值并将 amount
值作为第一个参数的函数替换 transactionCompleteCallback
函数。
bind() 将创建一个新方法但不会修改该方法,因此您的第一行应该是
transactionCompleteCallbackImpl = transactionCompleteCallbackImpl.bind(this, amount);
试试吧!