2013-10-24 50 views
0

這已經不夠接近這個了......至少我的新手頭腦可以把握。在控制檯中導致未定義錯誤的對象

我只是試圖創造一個全球包含對象具有方法,幷包含在內的是創建和使用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); 
+0

錯字:'globOject.counterClick(0),TRUE);'應該是'globObject.counterClick(0),TRUE);' – lastr2d2

+0

這個問題似乎是題外話,因爲它只是一個錯字。 – Barmar

+0

對不起,我錯過了那些傢伙,我現在很難過。 :( –

回答

0
  1. 修正我在評論中提到的錯字,也提到@Bonakid。 globOject.counterClick(0), true);應該globObject.counterClick(0), true);

  2. 不要使用globObject.counterClick(0)的回調函數,使用globObject.counterClick代替。當定義了globObject時,第一個會立即被呼叫。

  3. 請勿使用this.sendToServer();,請改爲使用globObject.sendToServer();this中的counterClick方法將爲document.getElementById('submbut')這是一個HTML元素。將console.log(this)添加到counterClick,您將看到。

A working demo here

+0

哇!最簡單的東西,謝謝。 –

0

變化這

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

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

那裏有一個拼寫錯誤在這部分

globOject.counterClick(0) 
相關問題