Friday, March 15, 2013

Object Oriented JavaScript Using JQuery

Javascript becomes more and more popular these days, and what best way to write reusable code if not writing it the OO style. JS is not your tipical programming language, the classes in js are not actualy classes, but funcions. Every function has a prototype object that contains methods and properties, we can use this object to write javascript classes.
1. Creating a JS Class:
To create a js class you need to create a function and after that you can use the this keyword to access the object's properties and methods.

2. Inheritance
Basicaly to inherit a class you need to extend the parent's class prototype object. To do that we can use jQuery.extend() function.
To call a parent method in the context of the child class you can use the call function:
3. How to extend the jQuery Class
After creating the object in the constructor we have to set the object's constructor to jquery's constructor, because jquery is using it internaly.

No comments:

Post a Comment