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, 5:12 AM EST
Nice article sir easy to understand
Nitesh KejriwalPosted Jul 6, 2015, 12:46 AM EDT
Great article Sandeep
Abhishek KumarPosted Apr 13, 2015, 4:24 AM EDT
Nice article..Thanks
Sandeep Singh ShekhawatPosted Apr 8, 2015, 8:28 PM EDT
Thank both of you :)
NitinPosted Apr 8, 2015, 6:12 AM EDT
Good one
Ratnesh SinghPosted Apr 8, 2015, 4:16 AM EDT
nice
Sandeep Singh ShekhawatPosted Apr 6, 2015, 8:18 PM EDT
Thanks both of you.
Sunny SharmaPosted Apr 6, 2015, 8:00 PM EDT
nice share!
Santhakumar MunuswamyPosted Apr 6, 2015, 7:48 PM EDT
Thanks for nice one
Gowtham RajamanickamTOP 100Posted Apr 6, 2015, 12:13 AM EDT
sure sandeep
Sandeep Singh ShekhawatPosted Apr 5, 2015, 8:24 PM EDT
Welcome Sir.
Sandeep Singh ShekhawatPosted Apr 5, 2015, 8:23 PM EDT
Thanks Gowtham Rajamanickam, Please stay connected for more article in this series.
Dinesh BeniwalTOP 10Posted Apr 5, 2015, 7:31 PM EDT
Thanks for sharing.
Gowtham RajamanickamTOP 100Posted Apr 5, 2015, 7:08 PM EDT
nicely explained
Gowtham RajamanickamTOP 100Posted Apr 5, 2015, 7:08 PM EDT
great