Introduction
JavaScript and HTML are well-known languages for web application development, and as a web developer, it’s been a long time I have been working with these two languages.
As a web application architect and developer, we have to take care of both server and client-side programming languages. I have worked with a lot of web applications/websites where I was involved in design and development. You may be aware of there are a lot of OOPS practices and principles which you have been implementing in your application in order to deliver successful projects
There are SOLID principles and well-known design patterns. I have seen and implemented a few of them in my practice on Server Side programming (mostly C#), but I found a lack of this principle and patterns in client-side programming languages like JavaScript.
However, it’s been a while when we got good frameworks for client-side development, and it’s worth to mention JQuery, Backbone, Ember, and AngularJS which are open source and come up with a web established framework to deliver rich client application.
In most of the cases, I have seen when the time comes to write some client-side code using JavaScript or JQuery, most of us write all the JavaScript functions and variables in the same HTML/cshtml/aspx/jsp/php document where those are required. For example:
Here is a function to perform client-side validation for age,
- function validateAge()
- {
- var age = parseInt($('#age').val());
- if (age < 20)
- {
- $('#error').html('Please enter a valid age');
- }
- }
Create a module for your application,
- var validationModule =
- {
- config:
- {
- ApplicationName: 'Default',
- validationEnabled: true
- },
- validateAge: function(element)
- {
- if (this.config.validationEnabled == true)
- {
- return (parseInt($(element).val()) > 20);
- } else
- {
- throw 'Validations are disabled';
- }
- }
- };
If you want to use this inside script you can use it like:
- var myApp= validationModule;
- var isValidAge = myApp. validateAge($('#age'));
- Global variable
- var VERSION=1.2.0;
- Private variable
- function getDateOfBirth()
- {
- // private variable
- var age = parseInt($('#age').val());
- var dob = (new Date().getFullYear()) - age;
- return dob;
- }
- var config =
- {
- user: 'Anupam'
- }
- function VerifyUser()
- {
- if (config.user == 'Anupam')
- {
- var text = 'hi ' + config.user;
- } else
- {
- // invalid user
- }
- // text has scope beyond if block
- console.log(text);
- }
- VerifyUser();
- console.log(text); // now undefined
- var validationModule = (function()
- {
- // current scope to variable module
- var module = this;
- // private variable
- var defaultConfig =
- {
- ApplicationName: 'Default',
- validationEnabled: true
- };
- module.getConfig = defaultConfig;
- module.setConfig = function(config)
- {
- defaultConfig.ApplicationName = config.ApplicationName;
- defaultConfig.validationEnabled = config.validationEnabled;
- };
- module.validateAge = function(age)
- {
- if (defaultConfig.validationEnabled == true)
- {
- return (parseInt(age) > 20);
- } else
- {
- throw 'Validations are disabled';
- }
- }
- // return module
- return module;
- })();
- (function(window, $)
- {
- // work with global window and JQuery object here
- })
- (window, JQuery);

Asfend YarPosted Mar 3, 2016, 3:42 PM
nice
Humayun Kabir MamunPosted Dec 28, 2015, 1:56 AM
Nice...
Anupam SinghPosted Dec 27, 2015, 12:40 AM
thanks guys
Sibeesh VenuPosted Dec 26, 2015, 10:50 AM
Nice Share
Gowtham KPosted Dec 26, 2015, 9:16 AM
Good One, Thanks for sharing
Banketeshvar NarayanPosted Dec 26, 2015, 6:00 AM
Good One
Raja TPosted Dec 26, 2015, 5:47 AM
Good, Thanks for sharing
Santhakumar MunuswamyPosted Dec 25, 2015, 3:23 PM
Thanks for nice article