2011-03-29 66 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框架中使用相当频繁。)