This question has already been answered:

import ws = require('ws'); export class Server { public static instance:Server; private _webSocketServer: ws.Server; // подключенные клиенты private _clients: { [id: number]: ws; } = { }; constructor() { if (Server.instance !== null && Server.instance !== undefined) throw new Error('singleton'); Server.instance = this; this._webSocketServer = new ws.Server({ port: 8081 }); this._webSocketServer.on('connection', this.onUserConnect); this._webSocketServer.on('error', this.onError); } private onUserConnect(client:ws): void { var id:number = Math.random(); this._clients[id] = client; console.log("новое соединение " + id); //client.on('close', ServerGame.instance.onUserDisconnect); client.onclose = this.onUserDisconnect; client.onmessage = this.onUserMessage; } 

I decided to make the nodejs server into typescript and divide everything by OOP into classes. I wrote such code in the typescript in the constructor, everything is fine ... _clients is initialized, but as soon as I get to onUserConnect, then _clients is undefined like the onUserConnect method, for example. Those. The feeling that I got into another empty object is somehow. not the class instance where it was. There are two questions here:

  • Is my mistake here (I'm new)? and how to fix everything in such a scheme?

  • Maybe there is a better approach where you can make a good OOP structure without such problems.

I tried to refer through a singleton and everything worked, but it's kind of dirty (Thanks in advance for your help!

Reported as a duplicate by Grundy members, Duck Learns to Hide , Community Spirit 16 Sep '16 at 6:50 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • It looks like you have a complete mess in your head and you do not understand the basic basics of the language. You just lost context in the handler. This js mechanism is not unique to type scripts. - Duck Learns to Take Cover
  • one
    This question and the answers to it can help you: www.stackoverflow.com/questions/535030/… - Duck Learns to Take Cover
  • porridge in the head is understandable ... I came from another language, where such behavior would not exist. I need help how to organize everything here) - voidmain
  • Functions in js are first class objects. when you do smth.on ('event', handler), you pass the handler itself without reference to the context. Read the answers on the link above that I brought, there is the same question only without too much. The problem of “loss of context” is typical for beginners in js and is explained in about every textbook on js, for example, here: learn.javascript.ru/objects-more - Duck Learns to Take Cover
  • I basically understand that the context is lost, but I do not understand how to transfer a callback with the context preserved. can you give a simple example? If I call the call method, this is the call and not the transfer of the method as a parameter. - voidmain

1 answer 1

TypeScript is not devoid of Javascript features when working with this .

More information about the possible reasons for the loss of context can be found in the answers to the corresponding question.

The important difference with TypeScript at the moment is that it allows using arrow functions when defining class members not only in the constructor, but also in the class body itself. In JavaScript, this is still possible only in the constructor.

 class test{ // Use arrow functions func1=(arg:string)=>{ return arg+" yeah" + this.prop; } // some property on this prop = 10; } 

Online example

  • "allows the use of pointer functions when defining class methods" is not true. This is just an instance field, not a class method. - Qwertiy
  • @Qwertiy, yes :-) did not pay attention to it. The syntax is a little different. How then is it better to write? - Grundy
  • And the classic version with closure or bind is no longer an option? - Qwertiy
  • @Qwertiy, option :-) that is why I voted for the duplicate. Here I just wanted to add that you can use the arrow functions in the class body itself. and not only in the constructor, as in javascript - Grundy
  • But after all, when compiling this into the constructor, it will go ... - Qwertiy