What's the best way for global variables in UXP development?

With the ES6 standard of UXP, which is the best way to set a global variable in my scripts?

If you really have no other choice you could bind to the window object. But avoid it if you can. It will make your code cleaner and work easier and less likely to be in conflict with 3rd party libraries that could accidentally try to use the same variable name.

Hey, thanks for your quick feedback. Do you think it would be better to redefine the variable

(for example …var app = require(‘photoshop’).app; …)

in every single function? My thought was that it would be practical to define it once globally, especially when trigger the application.

Well the best practice is not to do that. And in modern IDE like VSCode with TypeScript it can import things automatically. But otherwise, if you really want to then you can do it. The problem could if anything else will also try to add the same variable into the global scope.

1 Like

All right,
I will try my best :wink: