There is nothing wrong with your approach if:
- Download the script submodule after the script module
- You are not trying to access the script submodule before loading it
- You are fine so that your core module depends on the existence of a submodule. (I'm not sure this is a good idea.)
Side question
Your code currently has a syntax error in the following line:
var Module.Utils = (function() {
There should not be a var
keyword preceding the assignment.
Code example
Here's a simplified version of your code - disabled to show only the methods that I call - which demonstrates that your approach works:
var Module = (function() { function B() { console.log("Module: B"); Module.Utils.C(); }; return { B: B }; })(); var Module = Module || {}; Module.Utils = (function() { function C() { console.log("Module.Utils: C"); }; return { C: C } })(); Module.B();
Output:
Module: B Module.Utils: C
source share