Multifile extensions with global variables that are not constant



I'm writing an extension with multiple .js files, but where some need
access to variables in others. These variables are not constant.

As an example, in the main `extension.js`, imagine the following occurs:

// load the other .js files, theme.js & toon.js
const Theme = Extension.theme;
const Toon = Extension.toon;

var theme = new Theme.Theme();
var toons = [];
for ( let i=0; i<10; ++i ) {
    toons.append( new Toon.Toon() );
}

In my situation, the Toon.Toon class needs access to the 'theme'
variable to get some information from it.
However if I just use 'theme.[property]' from within toon.js without
declaring theme first, this is a syntax error (understandably).

Is the only way to give the `Toon.Toon` class access to `theme` to
provide it in the initialiser?:
    toons.append( new Toon.Toon(theme) );

Or is it possible to put in the toon.js file something like
    const GLOBAL = (????); // any variables that are shared between
files live here
and then have `theme.js` populate `GLOBAL.theme` with a pointer to the
`theme` object, and have `toon.js` access (the updated!)
`GLOBAL.theme`?

cheers.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]