I’m a novice at JS, and I’m trying to organise my app with a module kind of system but can’t seem to figure something out. In the example below, I define modules in the app.modules object and those get auto run by app.init(). I’m trying to pass app.settings and app.utils to those functions and achieve it in the following code, however the order has to be the same as those that I pass to it. So I couldn’t just use utils if I didn’t want settings. I’m trying to make them optional, so that if I only need one, I can ask for it and don’t get passed the others. Kind of like how you can pass things to angular controllers if and when you need them.
I hope that explains it well enough, I struggled at the title. And the example below is a simplified version, there could be more vars like settings and other objects like app.utils.
Whilst I do agree the use of a module loader system would be the best option, I suppose I was primarily asking how to accomplish the above, like what angular does as you’ve mentioned.
I could of, I guess, asked simply how the .module() function works, because although it’s similar to apply I don’t think it works quite the same way. At least to what I’ve done, or as I’m understanding. If the above code was written as:
Then utils would still work, and settings would be undefined I think, wouldn’t it? Whereas in my code, utils would now be a reference to the passed in settings variable (and settings would still be undefined).
I tried to look in the source of angular, but couldn’t for the life of me find where it was defined…
Hope that makes sense, and thanks for the link, nice read.
This will work but has a problem that everything needs to be in a specific order for it to work, and your modules can’t require other modules. This should give you an idea of how this type of thing is done though.
I could of, I guess, asked simply how the .module() function works, because although it’s similar to apply I don’t think it works quite the same way.
When you use call you need to know all of the arguments, apply lets you use an array of arguments so can be used for a dynamic number of arguments here.