JavaScript Code Organization | Module Pattern

JavaScript Code Structure - Module Pattern

In previous article i was discussed about the Object Literal, Which has limitations that is public properties and methods.

JavaScript Code Structure - The module pattern overcomes some of the limitations of the object literal. It offers privacy for variables and functions as well as exposing a public properties and methods if desired.

If you already knows JavaScript Module Pattern, you can skip this article and visit next code standard approach JavaScript Prototype

Generation Next JavaScript Code Structure Part - 2

JavaScript with Object-Oriented Programming (OOP) like approach encapsulation and abstraction

Encapsulate - Encapsulation is the technique to convert large complex application code into small chunks to understand large program easily. Moreover encapsulation describes the idea of bundling data-members and member-functions that work on that data within one small unit.

Abstraction - Abstraction is used to handle complex logic by hiding from the end user. That enables the developers to implement more complex logic.

For example, When you are in office, needs to drink coffee, you will go to office pantry, switch on the coffee machine and make coffee. and make coffee with a coffee machine, which is a good example of abstraction.

<!doctype html> <html lang="en"> <head> <script src="jquery.js"></script> </head> <body> <div class="dvResult"> Result: </div>   <script> 'use strict' // An object literal var yourVariable = (function(){ // Private variables and functions var yourProperty = "Hello World"; var yourPvtMethod = function() { $('.dvResult').append(yourProperty); }; var initPvt = function(settings) { yourProperty = settings; }; var readPvtSettings = function() { $('.dvResult').append(yourProperty); }; // Public Access return { yourPubMethod:function(){ yourPvtMethod(); }, initPub:function(arg){ initPvt(arg); }, readPubSettings:function(){ readPvtSettings(); } }; })(); yourVariable.yourPubMethod(); yourVariable.initPub(' by SW Class.'); yourVariable.readPubSettings(); </script> </body> </html>

Web Browser Response

Result: Hello World by SW Class.

Because the variables are defined inside function, we don't have access to them outside of the function unless we put them in the return object. It means that no code outside of the function will be access to the private properties and function. However, yourPubMethod, initPub and readPubSettings does have public access because they are defined in the same scope of return object.

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.

Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.



Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply