Monday, March 04, 2013

OOPS with JS - Part 3

in the 2nd part i have covered few basics about prototype object. some important points about prototype object in JavaScript.
  1. Every object have prototype property
  2. It holds an empty Object
  3. you can extend this object any time.
  4. you can have a constructor for this object.
before discussing more... encapsulation, inheritance, polymorphism, abstraction these are the principles of the any object oriented programming language. 

encapsulation is nothing but exposing or hiding your application objects / variables / components... to out side world (nothing but creating private / public methods, properties, variables, constants, lists... so on...
i will explain this by relating with real world...

when we saw the car on a road what we will see first - nothing but car body, wheels, seats, lights, bumpers..... we can't see engine inner parts unless we are dismantle it. so inner parts of the car like engine, hydraulic system, gear system, break systems are not seen by the out side world (nothing but encapsulation).

inheritance is a way to reuse the code of existing class or an object.every car is manufactured based on a engineering design..(this engineering design is nothing but a template - in our terms it is a class).

we can made N number of cars with the same template. we can have same characteristics for every car made based on that template. we don't required new templates for each car. we will have one template and reuse that to produce new ones (nothing but inheritance).

polymorphism means having more than one form...to simplify this --- a car may have lights. these lights can be used for different proposes, some time they might be used for singling the other vehicles on the road. night times they might be used for to view the road. same object but we are using for different proposes.

abstraction means recognizing common features and procedures of objects...to simple understanding... relating to existing world... every vehicle have wheel count... that may be a car, truck, auto....

we can simulate all above features in JavaScript we will have a look in the next post.....

 

Thursday, December 20, 2012

OOPS with JS - Part 2

in the first part i covered few points about encapsulation and creating constructor methods. now it's time to dig more.

for novices: in JavaScript we can create the objects with new operator and with object literal .

//with new operator
var myObject = new Object()// creates an empty object;
var myEmployee = new Employee() // creates an Employee object;

//with object literal notation
var myObject = {} // creates an empty object;
var myEmployee = {name:"srinivas"} //created object with name property.

we will use . operator to access the properties and methods on an object.
alert(myEmployee.name) //srinivas

now it's time to create a base class / parent class.(as usual we need to create a function nothing more else).

function Parent(){
this.whoAmI = "parent";
}
we created a function called Parent. remember JavaScript functions are objects. also remember objects may have properties and methods. so our Patent function also an object (don't be scared just imagine every thing is an object). Parent function is having some methods and properties. let's check some important properties and methods.

Properties
  1. constructor
  2. arguments // deprecated
  3. arity
  4. length
  5. name
Methods
  1. apply
  2. call
  3. toString
some more browser, JavaScript engine specific functions also available. but above are most important. i will discuss each of them when ever they required. apart form these, there is one  more rule of thumb : in JavaScript every object have a property called prototype. that holds an object.

//createing a object
var myParent = new Parent();

//whoAmI is called as object's own property
alert(myparent.whoAmI)//parent

//our parent function also an object so we will have a prototype property for this.
alert(typeof(Parent.prototype))//object