Window object natively

Hello guys. I was just wondering if it’s possible to use the window object to store global variables. I know it’s possible when I run the game in a browser, but will it still work when I compile it for Android/iOS/Windows?

you can console.log(window) to check.

Hi @ElPez,

I use it and it works fine on Browser and Android. I’m 99 percent sure, it works on iOS as well.

Best regards,
Zsolt

What’s the reason for using window? You can just use a static class to store values that are accessible over different scenes for example. It’s normally frowned upon but it does have it’s uses.

Today will be my first time building a project, I just wanted to make sure before going through all the process. Thank you very much @PZsolt27 .

Hi @phero_constructs, thanks for your reply.

You mean a static class like this MDN? For me it’s just a matter of taste, I prefer to stick to ES5 to ensure compatibility. I’m sure that Cocos Creator supports newer features outside ES5, but I have no idea if it relies on them, but the way JS is being adopted for an increasingly amount of tasks I think it’s a good idea to get into these new characteristics. I’ll definitely take a look at this.

I mean something like this

export default class GameState {
  static MY_VAR = 'MY_VAR';
}

Then you can just reference it with GameState.MY_VAR where ever you want as long as you have it imported.

That’s typescript though. I have no idea how you would do that in ES5.

hi,
I was wondering how one can export a class (not as default though) from js and import it in ts.
thanks!