OOP’s is one of the most popular ways in field of programming. Before OOP’s, list of instructions will be executed one by one. But in OOP’s we dealing with object that communicate to each other.

JavaScript supports OOP’s but not in the same way as other OOP languages like Java ,C++ etc.

The main difference between JavaScript and the other OOP’s languages is that, there are no Classes in JavaScript where classes are very important in creating objects.. However there are ways through which we can simulate the Class concept in JavaScript.
Another important difference is Data Hiding. There are no access specifier like (public, private and protected) in JavaScript but we can simulate the concept using variable scope in functions.

Concepts of OOP’s that we can achieve using JavaScript.

  • Object :-We can create object in JavaScript like this.

 

    //1)Creating Object through literal
    var obj={};
    //2)Creating with Object.create
    var obj= Object.create(null);
    //3)Creating using new keyword
    function Person(){}
    var obj=new Person();
  • Example of Class :-

<body onload="javascript:testing()">
<script type="text/javascript">
function Person(){
//Properties
this.name="Praveen Kumar";
this.age="27";
//functions
this.sayHi=function(){
return this.name +" Says Hi";
}
}
//Creating person instance
var p=new Person();
alert(p.sayHi());
</script>
</body>

  • Example of Constructor
<body onload="javascript:testing()">
<script type="text/javascript">
       function Person(name,age){
    //Assigning values through constructor
    this.name=name;
    this.age=age;
    //functions
    this.sayHi=function(){
    return this.name +" Says Hi";
    }
    }
    //Creating person instance
    var p=new Person("Praveen",27);
    alert(p.sayHi());
    //Creating Second person instance
    var p=new Person("Anand",26);
    alert(p.sayHi());
    </script>
</body>
  • Example of Inheritance

function Student(){}
//1)Prototype based Inhertance
Student.prototype= new Person();
//2)Inhertance throught Object.create
Student.prototype=Object.create(Person);
var stobj=new Student();
alert(stobj.sayHi());

  •  Example of Encapsulation :-
    <body>
<script type="text/javascript">
function Person(){
//this is private variable
var dob="13 Aug 1987";
//public properties and functions
return{
age:"27",
name:"praveen",
getDob:function(){
return dob;
}
}
}
var pobj=new Person();
//this will get undefined
//because it is private to Person
alert(pobj.dob);
</script>
</body>
  • Example of Polymorphism :-
<body>
<script type="text/javascript">
function Person(){
this.sayHI=function(){}
};
//This will create Student Class
function Student(){};
Student.prototype=new Person();
Student.prototype.sayHI=function(l){
return "Hi! I am a Student";
}
//This will create Teacher Object
function Teacher(){};
Teacher.prototype=new Person();
Teacher.prototype.sayHI=function(){
return "Hi! I am a Teacher";
}
var sObj=new Student();
//This will check if the student
//object is instance of Person or not
//if not it won't execute our alert code.
if (sObj instanceof Person) {
alert("Hurry! JavaScript supports Polymorphism");
}
</script>
</body>