Expanding on this answer I would like to know how to create new modules that belong to the same namespace (PROJECT).
A.init(); -- will become --> PROJECT.A.init();
Modules
(function( A, $, undefined ) {
A.init = function() {
console.log( "A init" );
};
}( window.A = window.A || {}, jQuery ));
(function( B, $, undefined ) {
B.init = function() {
console.log( "B init" );
};
}( window.B = window.B || {}, jQuery ));
A.init();
B.init();
http://jsfiddle.net/sKBNA/
Just insert the additional namespace into the property chain:
// create top namespace
window.PROJECT = window.PROJECT || {};
// stays the same
(function( A, $, undefined ) {
A.init = function() {
console.log( "A init" );
};
// except for the last line:
}( window.PROJECT.A = window.PROJECT.A || {}, jQuery ));
// same for the B (sub)namespace:
(function( B, $, undefined ) {
…
}( window.PROJECT.B = window.PROJECT.B || {}, jQuery ));
// and of course add it at the invocations:
PROJECT.A.init();
PROJECT.B.init();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With