繁体   English   中英

对象在控制台中导致未定义的错误

[英]Object causing an undefined error in console

目前还没有足够的东西……至少我的新手头脑可以把握。

我只是在尝试创建具有方法的全局包含对象,并且其中包含创建和使用AJAX请求的说明。 然后,我继续为按钮创建事件监听器。 然后,我得到以下控制台输出。

Port: Could not establish connection. Receiving end does not exist. -This was deleted-.net/:1
Uncaught ReferenceError: globOject is not defined script.js:21
Port: Could not establish connection. Receiving end does not exist. -This was deleted-.net/:1
Exception in onResRdy: TypeError: Cannot read property 'htmlRes' of undefined ContentScript.js:84

我确定我在这里做错了很多。 让我知道我需要包括哪些其他信息以获得帮助。

var globObject = {
sendToServer: function () {
    alert('you got to me at least!');
    var xhr;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xhr=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xhr=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.open('POST', 'counterDoc.php', true);
    xhr.send();
},
counterClick: function (counter) {
    alert('you got here!');
    counter += 1;
    this.sendToServer();
}};
var button = document.getElementById('submbut').addEventListener('click',globOject.counterClick(0), true);

改变这个

var button = document.getElementById('submbut').addEventListener('click',globOject.counterClick(0)

var button = document.getElementById('submbut').addEventListener('click',globObject.counterClick(0)

这部分有印刷错误

globOject.counterClick(0)
  1. 修正我在评论中说过的错字,也提到@Bonakid。 globOject.counterClick(0), true); 应该是globObject.counterClick(0), true);

  2. 不要将globObject.counterClick(0)用于回调函数,而应使用globObject.counterClick 定义globObject时,第一个将立即被调用。

  3. 不要使用this.sendToServer(); ,请使用globObject.sendToServer(); 代替。 thiscounterClick方法将是document.getElementById('submbut')这是一个HTML元素。 console.log(this)添加到counterClick ,您将看到。

这里的工作演示

暂无
暂无

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

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