Global variable in a single ts file

I have the following code for a class:

export class CC {
connected: any;

constructor() {
}

ngAfterViewInit() {
this.connected = 0;

var socket = new WebSocket(“ws://…”, “…”);
socket.onopen = function() {
//How to access myvar here?
//this.connected = 1;
}
}
}

I can’t manage to access the global variable connected in the onopen function. It can work in a “standard” javascript file. How can I fix this problem?

you want something like provider?

What kind of return type does socket.onopen have? Is it a promise, observable or something else? Can you subscribe to it instead of doing = function()?

If not you can always try something like this.

var self = this;
var socket = new WebSocket(“ws://…”, “…”);
socket.onopen = function() {
   //How to access myvar here?
   self.connected = 1;
}

This code breaks three of my rules. The last one is the proximate cause of your problem.

  • don’t abuse any
  • never type var
  • never use function inside of one

Use a lambda or arrow function to avoid losing execution context.

1 Like

@cfarren Perfect! Thank you very much!

Ugh. Why do I bother?

Look, you don’t have to respect me, you don’t have to agree with me, but if you are going to call something “perfect” and use your authority to mark it as a solution for all future visitors to see and emulate, can you please take a bit of time to address my concerns about the post you are lauding?

don’t worry i have the same feeling

My suggestion was only for if you can’t do all those other things I said.

Don’t take it personally. Thanks for your suggestions.

I think that everyone has their own coding choice. For instance, I think var is perfectly valid and is everywhere on the web.

I wrote “perfect” to @cfarren because his solution was an immediate fix and he was kind enough to write some code. You suggested a solution that is probably working but you didn’t provide code. Then do understand why I selected @cfarren answer instead.

I disagree strongly. See this topic for why. I do not find “is everywhre on the web” compelling in the slightest. The point of support forums like this is to guide people towards good coding practices, not popular ones.

Absolutely agree. I am trying to argue the merits of my opinions on this matter, and I am perfectly willing to let it stand in the court of public opinion.

I hope anybody in the future coming across this thread does not follow in your footsteps.

I guess I appreciate the fact that your primary concern is making something “work”. Mine is that future readers will spend less time going through the frustrations I went through, and will become more prolific programmers faster. I feel this thread is a net negative on that front.

I have read your reply. You made your point. I wrote in my previous reply “Thanks for your suggestions.” We disagree but I don’t intend to argue in order to avoid any infinite thread. Anyone who comes to this page with a similar problem should have one (or multiple) ways to fix their problem.

Thank you very much for your suggestions, i do really follow as much i can your posts cause they always teach me good programming pratices. You do a great job :+1: