Search This Blog

Thursday, July 11, 2013

Javascript user defined objects,inheritance and polymorfism



JavaScript is an Object Oriented Programming (OOP) language. A programming language can be called object-oriented if it provides four basic capabilities to developers:
  • Encapsulation . the capability to store related information, whether data or methods, together in an object
  • Aggregation . the capability to store one object inside of another object
  • Inheritance . the capability of a class to rely upon another class (or number of classes) for some of its properties and methods
  • Polymorphism . the capability to write one function or method that works in a variety of different ways


USER-DEFINED  Objects
I Approach.
in order to create user defined object we use the following frame of code:
       
         var newObject = new Object(); // Create the object
         newObject.firstProperty = "some string"; // Assign properties to the object
         newObject.secondProperty = "for example another string";

when we want to access this object we use the following frame of code:

document.write("firstProperty : " + newObject.firstProperty + "
");
document.write("secondProperty is : " + newObject.secondProperty + "
");

II Approach.(using user-defined function)

     function newObject (firstProperty , secondProperty){
this.firstProperty = firstProperty ;
this.secondProperty= secondProperty;
}
.....

var myNewObject = new newObject ("firstProperty value", "secondProp value");
document.write("firstProperty is : " + myNewObject.firstProperty + "
");
document.write("secondProperty is : " + myNewObject.secondProperty+ "
");

Assigning methods  to object.
Objects are composed of attributes. If an attribute contains a function, it is considered to be a method of the object otherwise, the attribute is considered a property.

function someFunctionOfThirdProperty(parammeter){ this.thirdProperty= parammeter; } function newObject (firstProperty , secondProperty){ this.firstProperty = firstProperty ; this.secondProperty= secondProperty; this.someFunctionOfThirdProperty=someFunctionOfThirdProperty;//!!!! } .... var myNewObject = new newObject ("firstProperty value", "secondProp value"); myNewObject.someFunctionOfThirdProperty("parammeterValue"); document.write("firstProperty is : " + myNewObject.firstProperty + " "); document.write("secondProperty is : " + myNewObject.secondProperty+ " "); document.write("thirdProperty is : " + myNewObject.thirdProperty+ " ");
with keyword
 with (object){
    properties used without the object name and dot
}
replace the function above with this function:
function someFunctionOfThirdProperty(parammeter){
     with(this){
       thirdProperty= parammeter; 
     }
}


Inheritance
All JavaScript functions have a property prototype, which is a reference to an Object. The purpose of this property is to implement inheritance
Let us suppose that we have the base class

function baseObject (firstBaseProperty , secondBaseProperty){
  this.firstBaseProperty = firstBaseProperty ;
  this.secondBaseProperty= secondBaseProperty;
}

let us suppose that we have the derivedObject

function derivedObject(firstDerivedProperty , secondDerivedProperty,thirdDerivedProperty){                                                this.firstDerivedProperty = firstDerivedProperty ;
this.secondDerivedProperty= secondDerivedProperty;
this.thirdDerivedProperty=thirdDerivedProperty;
}

in some other part of script we set up dynamical inheritance using prototype:
derivedObject.prototype = new baseObject ("fbpv1", "fbpv2")

and at the end 


var myDerivedObject= new derivedObject("fdpv1","fdpv2","fdpv3")

thuw the new obect has the following properties and values
firstBaseProperty=fbpv1
secondBaseProperty=fbpv2
firstDerivedProperty =fdpv1
firstDerivedProperty =fdpv2
firstDerivedProperty =fdpv3

also we can extend the functionality of derived object by adding some function:

derivedObject.prototype.displayDetails = function () {
 console.log(
this.firstBaseProperty+ ", " +
 this.secondBaseProperty+ ", " + 
this.firstDerivedProperty + ", " + 
this.firstDerivedProperty + ", " + 
this.firstDerivedProperty ); 
} 

and callthis in this way:

myDerivedObject.displayDetails();


Polymorfism
JavaScript polymorphism has a feature one does not find in all object-oriented languages. One can add and override members even on the fly, after an object has been created. An object can acquire or lose properties and behaviors over time

Let us suppose that we have the base class
function baseObject (firstBaseProperty , secondBaseProperty){
  this.firstBaseProperty = firstBaseProperty ;
                     this.secondBaseProperty= secondBaseProperty;   
          // private method
           function meth1() { return firstBaseProperty ; }
          // public methods
         this.meth2 = function(){ return this.secondBaseProperty; };
         this.meth3 = function(){ return meth1(); };
}
let us suppose that we have the derivedObject1 and derivedObject2
function derivedObject1((firstBaseProperty , secondBaseProperty,firstDerivedProperty 
, secondDerivedProperty,thirdDerivedProperty){
  baseObject .apply( this, arguments ); // initialize baseObject members
  this.firstDerivedProperty = firstDerivedProperty ;
  this.secondDerivedProperty= secondDerivedProperty;
  this.thirdDerivedProperty=thirdDerivedProperty;
  this.meth3 = function(){ return this.thirdDerivedProperty; } // override
   this.meth4 = function(){ return  firstDerivedProperty ; }
}
function derivedObject2((firstBaseProperty , secondBaseProperty,firstDerivedProperty1 
, secondDerivedProperty1,thirdDerivedProperty1){
                   baseObject .applythisarguments ); // initialize baseObject members
                    this.firstDerivedProperty1 = firstDerivedProperty1 ;
                    this.secondDerivedProperty1= secondDerivedProperty1;
                    this.thirdDerivedProperty1=thirdDerivedProperty1;
                    this.meth3 = function(){ return this.thirdDerivedProperty1; } // override
                    this.meth4 = function(){ return  firstDerivedProperty ; }
}
n some other part of script using prototype:

var myDerivedObject1= new derivedObject1("fbpv1", "fbpv2"."fdpv1","fdpv2","Niau niau")
var myDerivedObject2= new derivedObject2("fbpv11", "fbpv12"."fdpv11","fdpv12","gav gav")


var result1 = myDerivedObject1.secondBaseProperty; // parent property via child
 var result2 = myDerivedObject1.meth2(); // parent method via child
 var result3 = myDerivedObject1.meth3(); // child method overrides parent method "Niau niau"
        var result31 = myDerivedObject2.meth3();// child method overrides parent method "gav gav" var result4 = myDerivedObject1.meth4(); // child method

No comments:

Post a Comment

About Me

An seasoned developer, architect and with some Team Leading exposure, offering full project life cycle experiences within multi cultural, multi National environments and within differing business streams and all primarily on a .Net platform.