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