I've done a fair amount with vanilla javascript and jquery. I'm trying to write an app now that uses some form of closures to privatize methods so that everything isn't just out in the public namespace.
I turned to the Module Pattern, because it seemed to be the solution to my problems, however I can't quite wrap my head around it. When you use this pattern to add methods onto the main app, can all of the modules use all of the methods from other modules too? Because, it seems like that is not the case.
var MODULE = (function () {
var my = {},
privateVariable = 1;
function privateMethod() {
// ...
}
my.moduleProperty = 1;
my.moduleMethod = function () {
console.log("Method 1!");
};
my.anotherMethod(); //This doesn't work. anotherMethod() not defined
return my;
}());
var MODULETWO = (function (my) {
my.anotherMethod = function () {
console.log("Method 2!");
};
my.moduleMethod();
my.anotherMethod();
return my;
}(MODULETWO));
For some reason I would expect the commented method to work. Can we make that work? I envision having an app.js with the meat of an app, with other separate js files that have utility functions to help that app, but in a lot of cases I envision those utility classes needing access to the main app's scope. I can't seem to get that...
Thoughts?
Someone I talked to pointed me to Browserify.js, but I don't know if that solves my requirement of having everything inaccessible via closures. It seems just like a bundling app.
Aucun commentaire:
Enregistrer un commentaire