How to avoid JavaScript namespace pollution?

It seems to me that Unity JavaScript only offers a global namespace for custom JavaScript classes. Can this be circumvented?

I know that C# packages can be imported using the import directive. Is it possible to create packages in JavaScript as well?

I’m not sure if there is a namespace statement in UntiyScript. If there is you would have to write out all your classes as well as in C#, otherwise you can’t put them in a namespace.

Beside that Unity imports ALL classes derived from MonoBehaviour, ScriptableObject, Editor, EditorWindow and all other classes that are bound to the file name into the global namespace, always! Even when you import a dll which always contains a namespace, Unity extracts the MonoBehaviours during import. Namespaces are not supported by Unity. You can use them, especially for non Unity classes or to group your classes, but Unity classes will always be imported into the global namespace.

Unity doesn’t seem to support namespaces.

To illustrate the problem, if you have a script named “/Scripts/Intro/Play.js” and attempt to have another script named “/Scripts/Game/Play.js” then you will get a conflict.

I would suggest to use some sort of prefix / ‘hungarian’ notation instead.

E.g. “IntroPlay.js” vs “GamePlay.js”, or “I_Play.js” vs “G_Play.js”