Module Design Pattern — StudySection Blog

Module Design Pattern — StudySection Blog

The Module Pattern is the most common design pattern used in JavaScript. It keeps the particular code block independent from the other components. It wraps a set of variables and functions together in a particular scope. Module patterns enable better maintainability since all the related code can be encapsulated inside a single scope. This functionality is in the form of modules, and since we can reuse them, we do not need to define them again.

We can allow some properties and functions to be accessible from outside of the scope. as well as limiting the function and properties of the object itself will allow us to implement data hiding and abstraction.

Creating a module: Simple module patterns using an anonymous closure Anonymous closures are just functions that wrap our code in a single scope. Closures are one of the efficient features in JavaScript.
(function() {
// declare private variables and/or functions
return {
// declare public variables and/or functions
}
})();

Module pattern implementation with the public fields Here is the example:
function StudentDetails() {
var name: "Jon";
var age = 30;
var course = "Web Development"
return {
name: name,
age: age,
course: course
}
}
var newStudent = StudentDetails()
var userName = newEmployee.name;
var userAge = newEmployee.age;
var userDesignation = newEmployee.designation;

Now we will expand the above code, which restricts some variables to access outside the scope. Here is the example
function StudentDetails() {
var name: "Jon";
var age = 30;
var course = "Web Development",
var fee = 10000;
return {
name: name,
age: age,
course: course
}
}
var newStudent = StudentDetails()
// This will result in "undefined" value
var fees = newStudent.fee;

Data hiding using this pattern: The above code creates a new variable with the name “fees”, but it is not accessible outside of the class StudentDetails. This variable is seen as a private variable, which is why it is not accessible outside the scope.

Here is the example to access this variable from the outside the class with declaring it as a public:
function StudentDetails() {
var name: "Jon";
var age = 30;
var course = "Web Development",
var fee = 10000;
var calculateFees = function(amount) {
total = fee + amount;
}
return {
name: name,
age: age,
course: course,
calculateFees: calculateFees
}
}
var newStudent = StudentDetails()
var userName = newStudent.calculateFees(1000);

The Above code uses the private variable “fee” to calculate the total amount. So although the “fee” cannot be accessed directly using new student, it still remains in the scope so that other components can use this value for other calculations.

People having good knowledge of Financial accounting can get an Accounting Certification from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.

Originally published at https://studysection.com on March 15, 2022.

--

--

The most loved online platform for eCertification in several subjects including but not limited to Software Development, Aptitude and more.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Study Section

Study Section

The most loved online platform for eCertification in several subjects including but not limited to Software Development, Aptitude and more.