Many guys think that Javascript is a client side scripting language, its just used for client side form validations. But this proved false once AJAX come into picture. We will discuss some questions on this.
Is Javascript Object Oriented scripting language?
Many people will say no. But it is Object oriented. Javascript is a prototype-based object-oriented scripting language.
Now, another question arises, what is that “prototype-based”?
Wikipedia defines Prototype-based programming as a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based
languages) is performed via a process of cloning existing objects that serve as prototypes.
Anoter question arises “classes are not present”, then how will I create class and object????
In Javascript, your function = Class. hmmm, lot of confusions right?
I think it will be easy if we compare a programming language to understand it better. We will take Java as our reference language.
What is class?
A class is a construct that is used as a blueprint (or template) to create objects of that class.
What is Object?
An object is usually taken to mean a compilation of attributes (object elements) and behaviors (methods or subroutines) encapsulating an entity.
We will write a simple Class in Java
public class Theatre{
String theatreName;
int noOfSeats = 80;
public int getNoOfSeats() {
return noOfSeats;
}
public String getTheatreName() {
return theatreName;
}
public void setTheatreName(String newTheatre) {
this.theatreName = newTheatre;
}
}
Equivalent Javascript code
//Class Theatre
function Theatre(){
this.theatreName='PVR Cinemas';
this.noOfSeats = 80;
this.getNoOfSeats = function() {
return this.noOfSeats;
}
this.settheatreName = function(newTheatreName) {
this.theatreName = newTheatreName;
}
this.gettheatreName = function(){
return this.theatreName;
}
}
// Create an instance of the Theatre Class
var theatre = new Theatre();
alert(theatre.gettheatreName());
theatre.settheatreName('Hitman Theatre');
alert(theatre.gettheatreName());
alert(theatre.getNoOfSeats());
I think after comparing these two code samples, you guys were clear with the concept of class in Javascript.
How to use NameSpace in Javascript?
In Java language, package names are used to prevent naming collisions and also for organizing Java classes similar to the modules of Modula. In javascript this can be achieved by object hierarchies. We will see with an example
//create the base Package object if it does not exist.
if (!Package) {
var Package = new Object();
}
// define Person under Package
Package.Person = function () {
this.name='Albert';
this.age = 27;
this.changeHisAge = function(age) {
this.age = age;
}
}
//create an instance of the Person
var person = new Package.Person();
person.changeHisAge(28);
alert(person.name + ", age : " + person.age);
How to do Inheritance?
Here comes the keyword called “prototype”. Prototype is a type of inheritance in JavaScript. Its used to add a method to a class. Take the previous example. If we want to add a method “changeHisName”, then code follows below
Package.Person.prototype.changeHisName = function(name) {
this.name = name;
}
How to handle Visibility or Access level of my variables and methods?
Public properties this.variableName
Private Properties var variableName
Public Method ClassName.prototype.func_name = function() {}
Private Method var func_name = function() {}
Privilige Method this.func_name = function() {}
How to handle Singleton pattern in Javascript?
The singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. Its very simple, hold the current object in a variable, if it exists return the same object or create a new one. Implementation is as follows:
SingletonClass = function(name){
if(SingletonClass.object !== undefined)
return SingletonClass.object;
this.name = name;
SingletonClass.object = this;
}
var firstInstance = new SingletonClass('iSense');
alert(firstInstance.name);
//Alerts "iSense"
var secondInstance = new SingletonClass('Prabhu');
alert(secondInstance.name);
//Alerts alerts "iSense"
You can download the code used to explain.
Uses of Object Oriented javascript:
1. Reusability of the code increases
2. Dynamic loading of objects
3. Objects are organized better
Dude, start using object oriented javascript, Enjoy Coding