Window object natively

Window object natively
0.0 0

#1

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?


#2

you can console.log(window) to check.


#3

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


#4

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.


#5

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 .


#6

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.


#7

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.