Object-oriented programming (OOP) is a programming paradigm that uses abstraction to create models based on the real world. OOP uses several techniques from previously established paradigms, including modularity, polymorphism, and encapsulation.OOP promotes greater flexibility and maintainability in programming, and is widely popular in large-scale software engineering. Because OOP strongly emphasizes modularity, object-oriented code is simpler to develop and easier to understand later on. Object-oriented code promotes more direct analysis, coding, and understanding of complex situations and procedures than less modular programming methods.
Object: Every component in javascript is considered to be a object including the data types and function. Objects can be created either by object literal or constructor function.
lets say,
var person = {}
or
var Person = new object()
or
var Person = fuanction(name){ this.name=name;}
Namespace: This is like a container which contains all the data and functionality under a specific name. whatever object is created in javscript will be placed in global space unless we have specified the scope for it to exist. We can create a namespace as below.
var application= application || {}
Here application would be created if it doesnt exists if exists existing one would be used. Namespace is very important or else we may end up with namspace collision.
Class:
A class is a template definition of an object's properties and methods. We can create a class as below in javascript.
function Newclass(){
}
Inheritance : Before getting into inheritance let me explain a bit about protoype and how constructor behaves after overriding. We all know prototype is property of an object. Let say we have a Employee class
function Employee(name){this.name=name;}
Employee.prototype = {salary:'',grade:''}
Employee e1=new Employee(backend);
e1.constructor would give you Employee. Now check for e1.constructor.prototype.constructor which will give Employee which is actually not right. Now let me override the prototype.
Employee.prototype = {branch:'xx'};
Now if you try e1.branch you will get "undefined".Let me create a new instance say
Employee e2=new Employee(accountant); now if i try calling branch e2.branch it would give me xx and if I try calling e2.salary it says not a function. The prototype chain is live with the exception of when you completely replace the prototype object. So when you override its good to set the constructor property. In the below example I have a base class and child which wud inherit the common characters. The child class can be added by its own required properties.
function Parent(){
this.name=name;
}
function child(){
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
Encapsulation :
Polymorphism :
The programmer does not have to know the exact type of the object in advance, and so the exact behaviour is determined at run-time. This is what we call late binding or dynamic binding. Polymorphism can be implemented by creating two functions with same name with difference in arguments. we can invoke the function by checking the argument as shown below.
function fundTransfer(asianfund ,obj){
this.fund=asianfund;
alert(this.fund);
}
function fundTransfer(){
alert('domestic');
}
function f(obj5,obj) {
if(arguments.length == 2){
return fundTransfer(obj5,obj);
}
else{
return fundTransfer(obj5);
}
};
f('asian','international');
Object: Every component in javascript is considered to be a object including the data types and function. Objects can be created either by object literal or constructor function.
lets say,
var person = {}
or
var Person = new object()
or
var Person = fuanction(name){ this.name=name;}
Namespace: This is like a container which contains all the data and functionality under a specific name. whatever object is created in javscript will be placed in global space unless we have specified the scope for it to exist. We can create a namespace as below.
var application= application || {}
Here application would be created if it doesnt exists if exists existing one would be used. Namespace is very important or else we may end up with namspace collision.
Class:
A class is a template definition of an object's properties and methods. We can create a class as below in javascript.
function Newclass(){
}
Inheritance : Before getting into inheritance let me explain a bit about protoype and how constructor behaves after overriding. We all know prototype is property of an object. Let say we have a Employee class
function Employee(name){this.name=name;}
Employee.prototype = {salary:'',grade:''}
Employee e1=new Employee(backend);
e1.constructor would give you Employee. Now check for e1.constructor.prototype.constructor which will give Employee which is actually not right. Now let me override the prototype.
Employee.prototype = {branch:'xx'};
Now if you try e1.branch you will get "undefined".Let me create a new instance say
Employee e2=new Employee(accountant); now if i try calling branch e2.branch it would give me xx and if I try calling e2.salary it says not a function. The prototype chain is live with the exception of when you completely replace the prototype object. So when you override its good to set the constructor property. In the below example I have a base class and child which wud inherit the common characters. The child class can be added by its own required properties.
function Parent(){
this.name=name;
}
function child(){
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
Encapsulation :
A method of bundling the data and methods that use the data.Information hiding is a common feature in other languages often as private and protected methods/properties. Even though you dont have private publich protected methods like java/.net we can simulate something like this on JavaScript. when you create a member data in a function it becomes a private member. we can create objects with similar functionalities,you encapsulate the main functionalities in a Function and you use that Function’s constructor to create the objects. This is the essence of encapsulation
Polymorphism :
The programmer does not have to know the exact type of the object in advance, and so the exact behaviour is determined at run-time. This is what we call late binding or dynamic binding. Polymorphism can be implemented by creating two functions with same name with difference in arguments. we can invoke the function by checking the argument as shown below.
function fundTransfer(asianfund ,obj){
this.fund=asianfund;
alert(this.fund);
}
function fundTransfer(){
alert('domestic');
}
function f(obj5,obj) {
if(arguments.length == 2){
return fundTransfer(obj5,obj);
}
else{
return fundTransfer(obj5);
}
};
f('asian','international');
No comments:
Post a Comment