2016-07-05 78 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 

至极是未定义的,因为没有为该参考没有本地值/功能。

正如你所看到的,“这个”只有一个本地上下文总是绑定到它在爬上示波器时可以找到的第一个对象/函数。