2011-03-29 53 views
1

我有以下的JS對象:使用`this`在JS對象

var livePage = { 
    delay: 1000, 
    loadTables: function(){ 
     loadTable($("#vbeTable"),'getUpdateA') 
     loadTable($("#vbcTable"),'getUpdateB') 
     createAlertDialog(); 
    }, 
    setClicks: function(){ 
     $(".expand").live('click',function(){ 
      expand($(this).attr('expandvalue')); 
     }) 
     $(".launch") 
      .click(function(){ 
       newPopup('index.php',1120,550); 
      }); 
     $('.edit').live('click',function(){ 
      openColPick($(this).attr('colType')) 
     }); 
    }, 
    setRightClick: function(){ 
     $('body').contextMenu('mainmenu', { 
       bindings: { 
       'o_o': function(t) { 
        thePopupWindowsMain('oo','','',220,150,'right',''); 
       }, 
       'o_h': function(t) { 
        thePopupWindowsMain('oh','','',285,385,'left',''); 
       }, 
       'launch_prog': function(t) { 
        $(".launch").click(); 
       }, 
       'logout': function(t){ 
        window.top.location = 'logout.php'; 
       } 
       } 
      }); 
    }, 
    setWindow: function(){ 
     $(window) 
      .resize(function() { 
       $('body').css('height', $(this).height()) 
       alertToCorner(); 
      }) 
      .scroll(function(){$(this).resize()}); 
     $(window).resize(); 
    }, 
    checkLogout: function(){ 
     $.ajax({ 
      url: 'getLogin.php', 
      dataType: "html", 
      success: function(data){ 
       if($.trim(data) == 'LOGOUT'){ 
        window.location = 'logout.php'; 
       } 
      }, 
      complete: function(){ 
       setTimeout(function() { 
        livePage.checkLogout();}, 
       livePage.delay) 
      }, 
      timeout: 2000 
     }); 
    }, 
    init: function(){ 
     this.checkLogout(); 
     this.loadTables(); 
     this.setClicks(); 
     this.setRightClick(); 
     this.setWindow(); 
     console.log(this); 
    } 
} 

出於某種原因,在checkLogout: function()我必須使用livePage.delaylivePage.checkLogout()當我嘗試使用例如this.checklogout()我得到以下錯誤在Chrome的控制檯中:

Uncaught TypeError: Object [object DOMWindow] has no method 'checkLogout'

我該如何解決這個問題?

謝謝!

回答

5

函數內部this不受任何綁定在外面的約束。最簡單的解決方案是使用var self = this;分配給另一個變量,或者在您的情況下通過$.ajax()context: this選項傳遞變量。

+0

這工作:-)我添加了'var self = this;'到'checkLogout()'fn的頂部,並且我使用self而不是'this'謝謝^ _^ – Neal 2011-03-29 17:26:03

0

已使用Javascript完成對this this keyword的閱讀。在你的情況下,this指的是像錯誤說的窗口沒有方法'checkLogout'。

+0

爲什麼'this'在'init()'中工作? – Neal 2011-03-29 17:21:42

3

你可以試試,

checkLogout: function(){ 
     var self = this; //reference to the livePage object 
     $.ajax({ 
      url: 'getLogin.php', 
      dataType: "html", 
      success: function(data){ 
       if($.trim(data) == 'LOGOUT'){ 
        window.location = 'logout.php'; 
       } 
      }, 
      complete: function(){ 
       setTimeout(function() { 
        self.checkLogout();}, //need to use self because 'this' no longer refers to livePage in this context 
       livePage.delay) 
      }, 
      timeout: 2000 
     }); 
    } 
+0

與@ThiefMaster相同的回答它的工作原理^ _ _ ^ – Neal 2011-03-29 17:26:54

0

你應該一個context: this屬性添加到您發送給$.ajax,然後在完成處理程序調用this.checkLogout哈希值。

會發生什麼事是,JQuery的ajax方法將使用window作爲this情況下呼叫處理程序,我們可以通過添加context屬性來調用改變這種狀況。

1

this在js上遠遠不同於C#等語言。首先,它是功能範圍的。其次(也許更重要的是),你可以控制調用一個函數時會出現什麼樣的內容。 (查看「call」和「apply」函數,這些在javascript框架中使用相當頻繁。)