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
Post a Comment