2016-07-05 74 views
-1

訪問功能,通過這個

var Application; 
 
(function (Application, PhotonSdk) { 
 
    (function(Photon) { 
 
     Photon.PeerManager = (function() { 
 
      var $this; 
 

 
      function PeerManager() { 
 
       $this = this; 
 
       this.currentStatus = PhotonSdk.PhotonPeer.StatusCodes.connectClosed; 
 
       
 
       this.peer = new PhotonSdk.PhotonPeer("ws://localhost:9090"); 
 
       this.peer.addPeerStatusListener(PhotonSdk.PhotonPeer.StatusCodes.connecting, this._onConnecting); 
 
       this.peer.addPeerStatusListener(PhotonSdk.PhotonPeer.StatusCodes.connect, this._onConnect); 
 
      } 
 

 
      PeerManager.prototype.establishConnection = function() { 
 
       this.peer.connect(); 
 
       console.log("Photon is establishing connection."); 
 
      }; 
 

 
      PeerManager.prototype._onConnecting = function() { 
 
       this.currentStatus = PhotonSdk.PhotonPeer.StatusCodes.connecting; 
 
       PeerManager.prototype._logConnectionState(this.currentStatus); //It work 
 
      }; 
 

 
      PeerManager.prototype._onConnect = function() { 
 
       this.currentStatus = PhotonSdk.PhotonPeer.StatusCodes.connect; 
 
       this._logConnectionState(this.currentStatus); //It isn't work :(
 
      }; 
 

 
      PeerManager.prototype._logConnectionState = function (state) { 
 
       console.log("Photon connection is " + state + ". " + new Date().toTimeString()); 
 
      }; 
 

 
      return PeerManager; 
 
     })(); 
 
    })(Application.Photon || (Application.Photon = {})); 
 
})(Application || (Application = {}), Photon);

如果我使用this._logConnectionState(this.currentStatus); 我得到this._logConnectionState is not a function錯誤,但

PeerManager.prototype._logConnectionState(this.currentStatus); 

$this._logConnectionState(this.currentStatus); 

就是工作。爲什麼發生這種情況,以及我如何通過this做到這一點?

+0

當您使用的原型,它被綁定到一個對象。你將不得不實例化一個對象來使用它。默認情況下,'this'將指向全局作用域,這將不會有_logConnectionState – Rajesh

回答

0

我的建議是,事件_onConnecting和_onConnect由PhotonSdk.PhotonPeer實例分派。由於您在此處添加了聽衆:

this.peer.addPeerStatusListener(PhotonSdk.PhotonPeer.StatusCodes.connecting, this._onConnecting); 
this.peer.addPeerStatusListener(PhotonSdk.PhotonPeer.StatusCodes.connect, this._onConnect); 

所以函數被錯誤的調用。

試試這個:

function PeerManager() { 
    $this = this; 
    this.currentStatus = PhotonSdk.PhotonPeer.StatusCodes.connectClosed; 

    this.peer = new PhotonSdk.PhotonPeer("ws://localhost:9090"); 
    this.peer.addPeerStatusListener(PhotonSdk.PhotonPeer.StatusCodes.connecting, this._onConnecting.bind(this)); 
    this.peer.addPeerStatusListener(PhotonSdk.PhotonPeer.StatusCodes.connect, this._onConnect.bind(this)); 
} 
+0

是的,這是真的,謝謝,男人! – uda

0
 PeerManager.prototype._onConnect = function() { 
      this.currentStatus = PhotonSdk.PhotonPeer.StatusCodes.connect; 
      this._logConnectionState(this.currentStatus); //It isn't work :(
     }; 

你參考,你使用

_logConnectionState(this.currentStatus); 

上似乎是這樣的:

PeerManager.prototype._onConnect 

,而不是說:

Peer Manager.prototype 

basicly這是指

PeerManager.prototype._onConnect 

this._logConnectionState 

相同

PeerManager.prototype._onConnect._logConnectionState 

至極是未定義的,因爲沒有爲該參考沒有本地值/功能。

正如你所看到的,「這個」只有一個本地上下文總是綁定到它在爬上示波器時可以找到的第一個對象/函數。