2013-03-11 22 views
2

我只是想知道为什么一个在下面的脚本中使用临时变量“”(与当前分配的对象,即“”初始化):以初始化淘汰赛使用另一个变量可观测量

$(document).ready(function() { 

    function ChatViewModel() { 

     var that = this; 

     that.userName = ko.observable(''); 
     that.chatContent = ko.observable(''); 
     that.message = ko.observable(''); 
     that.messageIndex = ko.observable(0); 
     that.activePollingXhr = ko.observable(null); 


     var keepPolling = false; 

     that.joinChat = function() { 
      if (that.userName().trim() != '') { 
       keepPolling = true; 
       pollForMessages(); 
      } 
     } 

     function pollForMessages() { 
      if (!keepPolling) { 
       return; 
      } 
      var form = $("#joinChatForm"); 


      that.activePollingXhr($.ajax({url: form.attr("action"), type: "GET", data: form.serialize(), cache: false, 
       success: function(messages) { 
        console.log(messages); 
        for (var i = 0; i < messages.length; i++) { 
         that.chatContent(that.chatContent() + messages[i] + "\n"); 
         that.messageIndex(that.messageIndex() + 1); 
        } 
       }, 
       error: function(xhr) { 
        if (xhr.statusText != "abort" && xhr.status != 503) { 
         resetUI(); 
         console.error("Unable to retrieve chat messages. Chat ended."); 
        } 
       }, 
       complete: pollForMessages 
      })); 
      $('#message').focus(); 
     } 

     that.postMessage = function() { 
      if (that.message().trim() != '') { 
       var form = $("#postMessageForm"); 
       $.ajax({url: form.attr("action"), type: "POST", 
        data: "message=[" + that.userName() + "] " + $("#postMessageForm input[name=message]").val(), 
        error: function(xhr) { 
         console.error("Error posting chat message: status=" + xhr.status + ", statusText=" + xhr.statusText); 
        } 
       }); 
       that.message(''); 
      } 
     } 

     that.leaveChat = function() { 
      that.activePollingXhr(null); 
      resetUI(); 
      this.userName(''); 
     } 

     function resetUI() { 
      keepPolling = false; 
      that.activePollingXhr(null); 
      that.message(''); 
      that.messageIndex(0); 
      that.chatContent(''); 
     } 

    } 

    //Activate knockout.js 
    ko.applyBindings(new ChatViewModel()); 

}); 

为什么我不能只用“这个”?任何人都可以解释吗?

+0

了解更多关于'this':https://developer.mozilla.org/en-US/docs/JavaScript/Reference /运营/本。 – 2013-03-11 12:59:23

回答

5

this总是指在调用完成时处于范围内的对象,并且这可以根据您的代码进行更改。如果你希望它仍然是子函数中的对象,那么将它分配给一个不会改变值的变量就可以解决这个问题。

+0

太棒了!我现在明白了。谢谢。 – balteo 2013-03-11 13:03:13

1

这是指所有者。 你可以重写你的代码是这样的:

$(document).ready(function() { 

function ChatViewModel() { 

    var that = this; 

    this.userName = ko.observable(''); 
    this.chatContent = ko.observable(''); 
    this.message = ko.observable(''); 
    this.messageIndex = ko.observable(0); 
    this.activePollingXhr = ko.observable(null); 


    var keepPolling = false; 

    this.joinChat = function() { 
     if (that.userName().trim() != '') { 
      keepPolling = true; 
      pollForMessages(); 
     } 
    } 

    function pollForMessages() { 
     if (!keepPolling) { 
      return; 
     } 
     var form = $("#joinChatForm"); 


     this.activePollingXhr($.ajax({url: form.attr("action"), type: "GET", data: form.serialize(), cache: false, 
      success: function(messages) { 
       console.log(messages); 
       for (var i = 0; i < messages.length; i++) { 
        that.chatContent(that.chatContent() + messages[i] + "\n"); 
        that.messageIndex(that.messageIndex() + 1); 
       } 
      }, 
      error: function(xhr) { 
       if (xhr.statusText != "abort" && xhr.status != 503) { 
        resetUI(); 
        console.error("Unable to retrieve chat messages. Chat ended."); 
       } 
      }, 
      complete: pollForMessages 
     })); 
     $('#message').focus(); 
    } 

    this.postMessage = function() { 
     if (that.message().trim() != '') { 
      var form = $("#postMessageForm"); 
      $.ajax({url: form.attr("action"), type: "POST", 
       data: "message=[" + that.userName() + "] " + $("#postMessageForm input[name=message]").val(), 
       error: function(xhr) { 
        console.error("Error posting chat message: status=" + xhr.status + ", statusText=" + xhr.statusText); 
       } 
      }); 
      that.message(''); 
     } 
    } 

    this.leaveChat = function() { 
     that.activePollingXhr(null); 
     resetUI(); 
     that.userName(''); 
    } 

    function resetUI() { 
     keepPolling = false; 
     that.activePollingXhr(null); 
     that.message(''); 
     that.messageIndex(0); 
     that.chatContent(''); 
    } 

} 

//Activate knockout.js 
ko.applyBindings(new ChatViewModel()); 
//fixing bracet 
}); 

检查此链接:http://www.quirksmode.org/js/this.html