

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to understand JavaScript module pattern?
JavaScript does not support classes natively, so the Module pattern is used. This is to store public, private methods, and variables inside a single object. To use and understand it, we will workaround Anonymous closure, to display the voters disqualified since they failed in fulfilling the age criteria of 18 years.
Example
You can try to run the following code to understand the JavaScript module pattern
<!DOCTYPE html> <html> <body> <script> (function () { var votersAge = [15, 50, 27, 17, 22, 87, 65, 45]; var average = function() { var total = votersAge.reduce(function(accumulator, age) { return accumulator + age}, 0); return total / votersAge.length + '.'; } var notQualified = function(){ var notAdult = votersAge.filter(function(age) { return age < 18;}); return 'Voters not qualified to vote (age less than 18) = ' + notAdult.length; } document.write(notQualified()); }()); </script> </body> </html>
- Related Questions & Answers
- How to validate a date pattern in JavaScript?
- Number pattern in JavaScript
- How to understand Seaborn's heatmap annotation format?
- How to import and export a module/library in JavaScript?
- How to install PowerShell Module?
- How to search a string for a pattern in JavaScript?
- Python - Plotting an Excel chart with pattern fills in column using XlsxWriter module
- Example to understand type of variables in Java
- How to develop a Python Module?
- How to install a Python Module?
- How to write a python module?
- How to retrieve Python module path?
- How to uninstall the PowerShell Module?
- Which one is the Python module to obfuscate javascript?
- How to connect to an SAP module?
Advertisements