Jump to content
Nytro

JavaScript Object Oriented Programming(OOP) Tutorial

Recommended Posts

Posted

JavaScript Object Oriented Programming(OOP) Tutorial

Object Oriented Programming is one of the most famous way to do programming. Before OOPs there was only list of instructions execute one bye one. But in OOPs we will deal with Objects and how those objects t interact with one another.

JavaScript-Object-Oriented-Programming%28OOPs%29-Tutorial.png

JavaScript supports Object Oriented Programming but not the same way as other OOP languages(c++,php,Java,etc.). The main difference between these language and JavaScript is, there is no Classes in JavaScript where the classes are very impotent to create objects. But there is a way we can simulate the Class concept in JavaScript.

Another important difference is data hiding. There is no access specifiers like (public,private,protected) in JavaScript. Again we will simulate the concept using variable scope in functions.

Object Oriented Programming Concepts

1)Object

2)Class

3)Constructor

4)Inheritance

5)Encapsulation

6)Abstraction

7)Polymorphism

Preparing the work space

Create a new file "oops.html" and write this code on it. We will write all our JavaScript code on this file.

    <html>

<head>

<title>JavaScript Object Oriented Programming(OOPs) Tutorial</title>

</head>

<body>

<script type="text/javascript">

//Write your code here.....

</script>

</body>

</html>

1)Object

Any real time entity is consider as Object. Every Object will have some properties and functions. For example consider person as an object then he will have properties like name,age,etc. And functions as walk, talk, eat, think,etc.

Now lets see how we create objects in JavaScript. There are so many ways we can create objects in JavaScript. Some of them are

    //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();

We can use any of the above way to create Object.

2)Class

As I said earlier there on classes in JavaScript. Because JavaScript is Prototype based language. But we can simulate the class concept using JavaScript functions.

    function Person(){

//Properties

this.name="aravind";

this.age="23";

//functions

this.sayHi=function(){

return this.name +" Says Hi";

}

}

//Creating person instance

var p=new Person();

alert(p.sayHi());

3)Constructor

Actually Constructor is a concepts comes under Class concept. The constructor is used to assign values to the properties of the Class when creating object using new operator. In above code we have use name,age properties for Person class now will assign values while creating new objects for person class as below.

    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("aravind",23);

alert(p.sayHi());

//Creating Second person instance

var p=new Person("jon",23);

alert(p.sayHi());

4)Inheritance

Inheritance is a concept of getting the properties and function of one class to other class is classed Inheritance. For example lets consider "Student" Class. Now the Student also have properties name,age. We already have this properties in Person class. So it's much better to acquiring the properties of the Person instead of re-creating those properties. Now lets see how we can do inheritance in JavaScript.

    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());

We can do inheritance in above two ways.

5)Encapsulation

Before we learn Encapsulation and Abstraction first we need to know what is data hiding? and who can we achieve it in JavaScript. Date hiding means hiding the data form accessing it out side the scope. For example in Person class and we have Date of Birth(dob) properties and we want to hide it form out side. Let's see how can we do it.

    function Person(){

//this is private varibale

var dob="8 June 2012";

//public properties and functions

return{

age:"23",

name:"aravind",

getDob:function(){

return dob;

}

}

}

var pobj=new Person();

//this will get undefined

//because it is private to Person

console.log(pobj.dob);

//Will get dob value we using public

//funtion to get private data

console.log(pobj.getDob());

Now Encapsulation Means wrapping up of public and private data into a single data unit is called Encapsulation. Above example is one best suites Encapsulation.

6)Abstraction

Abstraction means hiding the inner implementation details and showing only outer details. To understand Abstraction we need to understand Abstract and Interface concepts from Java. But we don't have any direct Abstract or Interface in JS.

Ok! now in-order to understand abstraction in JavaScript lets take a example form JavaScript library JQuery. In JQuery we will use

$("#ele")

to select select an element with id ele on a web page. Actually this code calls negative JavaScript code

document.getElementById("ele");

But we don't need to know that we can happy use the $("#ele") without knowing the inner details of the implementation.

7)Polymorphism

The word Polymorphism in OOPs means having more than one form. In JavaScript a Object,Property,Method can have more than one form. Polymorphism is a very cool feature for dynamic binding or late binding.

    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 OOps");

}

Conclusion

JavaScript supports Object Oriented Programming(OOP)Concepts. But it may not be the direct way. We need to create some simulation for some concepts. 10 Aug 2013

by aravind buddha at 10:44 PM

Sursa:JavaScript Object Oriented Programming(OOP) Tutorial : Techumber

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...