提问人:ishandutta2007 提问时间:10/29/2023 最后编辑:ishandutta2007 更新时间:10/30/2023 访问量:76
为什么以下 JavaScript 函数绑定不起作用?
why is the following javascript function binding not working?
问:
我正在绑定到一个函数,其中我设置了然后我通过javascript将脚本注入页面。
但没有接到电话。myfunc
this.enforcement
myfunc()
class MyClass {
constructor() {
this.enforcement = undefined
window.myfunc = this.myfunc.bind(this);
const script = document.createElement('script')
script.src = "https://tcr9i.chat.openai.com/v2/35536E1E-65B4-4D96-9D97-6ADB7EFF8147/api.js";
script.async = !0;
script.defer = !0;
script.setAttribute('data-callback', 'myfunc')
document.body.appendChild(script)
}
myfunc(enforcement) {
this.enforcement = enforcement
alert("enforcement set")
enforcement.setConfig({
onCompleted: (r) => {
console.debug('enforcement.onCompleted', r)
},
onReady: () => {
console.debug('enforcement.onReady')
},
onError: (r) => {
console.debug('enforcement.onError', r)
},
onFailed: (r) => {
console.debug('enforcement.onFailed', r)
},
})
}
async getEforcement() {
if (!this.enforcement) {
alert("no enforcement found") // <- it is always reaching here since setting of `this.enforcement` inside `myfunc` is not happening
return
}
return new Promise((resolve, reject) => {
this.pendingPromises = [{ resolve, reject }]
this.enforcement.run()
})
}
}
我在页面加载时明确调用,但仍然没有被调用。getEforcement()
myfunc
作为调试步骤,我已经验证了脚本是否正确嵌入到我的指定页面中并且 src 正在工作,我在里面放置了一个断点并验证它没有进入它。我不确定如何调试事件未发生部分的触发。api.js
答: 暂无答案
评论
myfun
script.setAttribute('data-callback', 'myfunc')
api.js
myfunc()
window.myfunc = this.myfunc.bind(this);
script.setAttribute('data-callback', 'myfunc')
myfunc
document.body.appendChild(script);
Failed to load resource: net::ERR_FAILED