Introduction
Code reviews of JavaScript code is always a headache for web developers because large JavaScript code is not easy to read and understand so this article might help introduce how to write simple and readable code in JavaScript. To make things easy I am using jQuery here but I will show each thing here.
It is important to note that there are no classes in JavaScript because JavaScript is a prototypal object-oriented language. This means objects inherit from objects, not classes from classes as in object-oriented languages (C#, Java). Everything is an object except for the primitive data types (boolean, number, and string) and undefined.
A "class" is only a concept that supports the Object-Oriented Programming model in JavaScript. When a JavaScript function is invoked as a constructor with the new keyword then that function calls a class (conceptually).
A road map to learn object-oriented JavaScript
- Simplify JavaScript Object-Oriented Programming Model: Part 2
- Simplify JavaScript Object-Oriented Programming Model: Part 3
- (function($) {
- function Page() {
- var $this = this;
- function initialize() {
- $('body').css('background-color', 'rgba(123, 120, 234, 0.7)');
- }
- $this.init = function() {
- initialize();
- }
- }
- $(function() {
- var self = new Page();
- self.init();
- })
- }(jQuery))
Remove jQuery Conflicts
When we are developing a large project it's possible to use more than one JavaScript library and it's also possible for their aliases to conflict. You can remove conflicts using the following code that shows $ alias represents jQuery, in other words, $ is doing the same as jQuery so you can also replace $ by jQuery.
- (function($) {
- //code here
- }(jQuery))
Class and Global Variable
I have been previously mentioned that class is a concept in JavaScript. As in the following code sample we have created some functions but we invoke the Page() function by its constructor using the new keyword. That's why it's called a class instead of a function.
- function Page() {
- var $this = this;
- function initialize() {
- $('body').css('background-color', 'rgba(123, 120, 234, 0.7)');
- }
- $this.init = function() {
- initialize();
- }
- }
- $(function() {
- var self = new Page();
- self.init();
- })
Private and Public Function
As you know, the Page() is a function that is also a class but internally we create two more functions as shown in the following code snippet.
- function initialize() {
- $('body').css('background-color', 'rgba(123, 120, 234, 0.7)');
- }
- $this.init = function() {
- initialize();
- }
Call on Window Load
As in the following code, it's also a function that creates an object of the Page() class and calls its public method init().
- function initialize() {
- $('body').css('background-color', 'rgba(123, 120, 234, 0.7)');
- }
- $this.init = function() {
- initialize();
- }
This function is always called when the window loads, in other words it is similar to $(document).ready().
- $(function() {
- $(document).ready(function() { ==
- })
- })
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>index</title>
- </head>
- <body>
- <h1>Welcom JavaScript & jQuery !</h1>
- </body>
- </html>
- <script src="Scripts/jquery-1.7.1.min.js">
- </script>
- <script src="Scripts/page.js">
- </script>

Tarun SharmaPosted Mar 4, 2017, 10:12 AM
Nice article sir easy to understand
Nitesh KejriwalPosted Jul 6, 2015, 4:46 AM
Great article Sandeep
Abhishek KumarPosted Apr 13, 2015, 8:24 AM
Nice article..Thanks
Sandeep Singh ShekhawatPosted Apr 9, 2015, 12:28 AM
Thank both of you :)
NitinPosted Apr 8, 2015, 10:12 AM
Good one
Ratnesh SinghPosted Apr 8, 2015, 8:16 AM
nice
Sandeep Singh ShekhawatPosted Apr 7, 2015, 12:18 AM
Thanks both of you.
Sunny SharmaPosted Apr 7, 2015, 12:00 AM
nice share!
Santhakumar MunuswamyPosted Apr 6, 2015, 11:48 PM
Thanks for nice one
Gowtham RajamanickamPosted Apr 6, 2015, 4:13 AM
sure sandeep
Sandeep Singh ShekhawatPosted Apr 6, 2015, 12:24 AM
Welcome Sir.
Sandeep Singh ShekhawatPosted Apr 6, 2015, 12:23 AM
Thanks Gowtham Rajamanickam, Please stay connected for more article in this series.
Dinesh BeniwalPosted Apr 5, 2015, 11:31 PM
Thanks for sharing.
Gowtham RajamanickamPosted Apr 5, 2015, 11:08 PM
nicely explained
Gowtham RajamanickamPosted Apr 5, 2015, 11:08 PM
great