A short trip to Modular JavaScript patterns.
Modular scripting is a art of writing, reusing and maintaining independent block of JavaScript codes effectively with help of script loaders like requirejs, curljs for a application.
i feel comparing the puzzle pieces with modules will make more sense. programmers have to write, relate and connect the pieces to win a game.
Encapsulation is the another milestone which is achieved in JavaScript with help of module patterns. programmer can isolate the private content from the rest of world with help of modules.
Dependency is another option, which used to avoid loading unwanted codes in the application.
After surfing a bit about what? why? in Google, i have started thinking about how is it implemented? i have got lot of module patterns like CommonJS,AMD,UMD,ES6 modules…
CommonJS is synchronous,mostly it is on server side programming like NodeJS.
require and module.exports are the components on CommonJS.
module.exports is used to define a module and it decide the public contents.
require is used to load and use the modules which are available in public context.
In asynchronous(AMD) style, mostly on client side, AMD loaders like requireJs are used to define and use the modules.
define and require are used to define and use the modules. In general, all the module pattern, we can decide private and public contents, whatever properties, methods we return those are available for in public. i have tried to write same logic in all patterns. please refer below.
Modular Pattern with Object literal
Modular Pattern with object stub
Revealing Modular Pattern
Conclusion
Learning of modular pattern is going like ocean, till now i got to know it is ease to use, maintain, test and also gives the concept of encapsulation and dependencies in JavaScript. still exploring …