For example:
// a.js
goog.provide('mypackage.a');
goog.provide('mypackage.commands');
mypackage.a.somevar = 1;
mypackage.commands.save = function(...) {...};
// b.js
goog.provide('mypackage.b');
goog.provide('mypackage.commands');
mypackage.b.somevar = 1;
mypackage.commands.read = function(...) {...};
// mypackage/commands.js
goog.provide('mypackage.commands');
mypackage.commands.runCommand = function(commandText, args) {
return mypackage.commands[commandText](args);
}
Is this a good way to provide an extensible set of commands, or is there something that could make this complex that I’m not thinking about?
There is no reason that you can’t or shouldn’t provide the same module in different source files. If it makes sense to your source code organization scheme, then it’s a perfectly fine thing to do. One of the main reasons that we have
goog.provide()is so that the same symbol can be used in several different places, but defined in whichever file happens to run first.If I understand
goog.provide()correctly, all it does is make sure that an object is declared. So, goog.provide(‘mypackage.commands) makes sure thatmypackage.commands` is declared in the global scope.So
goog.provide('mypackage.commands');just accomplishes something similar to this:You only need to do that when you plan on adding things to that object in this source file. So, if multiple source files are all adding new items onto
mypackage.commands, then each source file would dogoog.provide('mypackage.commands)` to make sure that the right global variable structure is declared.That appears to be what you are doing in your code example and that is a perfectly fine thing to do. It is up to your own sense of code organization whether it’s better to have multiple source files all contributing to the same object (like you have) or whether you should organize your source files such that all code pertaining to one particular namespace is in the same file. That’s really up to you and how you think it is best to organize your source code – there is no right or wrong answer other than there should be some rhyme and reason behind how it’s organized.
Helpful reference article: https://developers.google.com/closure/library/docs/tutorial