brucemcpherson / desktopliberation

hosting for desktop liberation google plus community
30 stars 1 forks source link

Fix Apps Script file order problems with Exports #146

Open brucemcpherson opened 1 year ago

brucemcpherson commented 1 year ago

It’s good practice to keep class and namespace definitions in separate files and avoid defining functions or variables in the global space. However, App Script doesn’t give you control over the order in which it loads files. If you reference a class or a namespace from one script file, it may not yet be defined. This is where an Exports object comes in.

Using ‘var’ rather ‘const’ can help, as JavaScript var ‘hoists‘ both var and function declarations, but this won’t always solve the problem, especially with class definitions.

Many of my projects have 20 or more files and I kept running into this problem until I figured out this simple workaround. You also create a much cleaner, better documented and more flexible project.

Here’s how to use an Exports object which documents the contents of your script

https://ramblings.mcpher.com/gassnippets2/exports/