Typescript cannot retain reference to owner class -


i can't figure out how keep reference chat class in situation. what's workaround?

class chat {     private self: chat;     public currentusers: = ko.observablearray();      constructor(public chatservice: any) {         this.self = this;         chatservice.client.receiveusers = this.receiveusers;     }       private receiveusers(users: any): void {         //'this' has been changed refer external caller context (chatservice.client)         this.currentusers(users);//fail         //property currentusers not exist on value of type 'window'         self.currentusers(users);//fail         //currentusers not exist in current scope         currentusers(users);//fail         //there's apparently no way access anthing in chat class inside here?     } } 

trying keep reference this on class instance putting sticky note on remote control says "here's remote is!" because keep losing it.

use fat arrow lambda expression capture lexical 'this' @ callback site:

class chat {     public currentusers: = ko.observablearray();      constructor(public chatservice: any) {         chatservice.client.receiveusers = (users) => this.receiveusers(users);     }       private receiveusers(users: any): void {         // use 'this' here     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -