OOPS!! Am not screaming. I mean Object oriented programming.
OOPS here , OOPS there and OOPS everywhere. Does jQuery support object oriented programming?
The answer is a big YES*.
I put a * next to YES. Did you see that. It means Conditions Apply. We will talk about the conditions apply later.
Class Declaration
Let us create a simple class.
var simpleClass = { };
Thats it?. Attributes? Methods ?. Where are they?. Lets see how can we fit those in inside this empty class.
Attributes and Behaviours
var simpleClass = { testAttribute : 'test', // atttribute testMethod : function() //method { return testAttribute; } };
Now we instantiate this Object.
var simpleClassObj = new simpleClass();
Perfect!!!!.
Conditions Apply
Private Variable
There aint any private variables here. To access the testAttribute, just use
simpleClassObj.testAttriute;
Enough!!
Dynamic Attribute Insertion
Suppose, i use the statement
simpleClassObj.testAttribute2 = 'test2';
Do you think, it will work. compilation error??. Duh!. It will still work. Our exceptional system will modify the source class for us ( Amazing huh!!).
You can clearly see, jQuery has sold the encapsulation feature for duck typing. Only god knows, the actual state of an object here.
Why OOPS in jQuery
Now, the question. If not for encapsulation, why do we need to go for OOPS approach in jQuery. The answer is Organization.
Consider a simple example HTML;
<input type="text" name="test-name" id="test-id" class="test-class" style="test-style"/>
If you want to manipulate this dom object in jQuery , you go to do this
$('#test-id').val() // get value $('#test-id').attr('name') // get name $('test-id').html() //get inner HTML
Can you see, these three simple statements have absolutely no similarity (except the object part). Imagine a similar style of code , in a page which has plenty of dom objects and widgets. Do you think its easy to understand??.
Now lets convert this to a simple object
Lets create a simple abstract class ( Dont search for the abstract keyword. We dont have one. if you want an abstract class, better YOU dont instantiate it)
var syndrome = { doHide : function(){ this.hide('slow'); }, doShow : function(){ this.show( 'slow' ); }, doDisable : function(){ this.attr('disabled', 'disabled'); }, toggle : function(){ this.toggle(); }, getStyle : function(){ return this.attr('style'); }, setForeground : function(color){ var style = 'color:'+color+';'; this.attr('style', style+this.getStyle()); }, setBackground : function(color){ var style = 'background-color:'+color+';'; this.attr('style', style+this.getStyle()); }, getName : function(){ return this.attr('name'); }, getValue : function(){ return this.attr('value'); }, getClass : function() { return this.attr('class'); }, getContent : function(){ return this.html(); } };
Its easily readable, you can see what am doing. Just put some common methods. Now , lets see how can we use this in our above example.
var testObj = $('#test-id'); //get the dom object $.Extend( testObj, syndrome ); //extending the abstract class. So , the method should be available here. testObjtestObj.getValue(); // Get value testObj.getName(); // Get Name testObj.getContent(); // Get content
Now this looks neat and clean. Now Imagine the same scenario, a lot of components and widgets. What will you have . Just a list of $.Extend statements with pure object interaction.
Clean!. Try it!. you will love it.
nice one!!!
You cant instantiate an object like var simpleClass = { };
Try it!
You can
Paste the below code( just these line, no other things needed) in a notepad. Save it as HTML and open in browser. And see the magic!!
var obj = {};
obj.attr = 'Attribute Data';
alert(obj.attr);
Javascript does have private and public variables, at least you can mimic their behaviour by doing this:
var simpleClass = function(){
var abc = 1;
this.setabc = function(param){
abc = param;
}
this.getabc = function(){
return abc;
}
}
var objSimpleClass = new simpleClass();
objSimpleClass.getabc(); //should return 1
objSimpleClass.setabc(5);
objSimpleClass.getabc(); //now returns 5
var objSimpleClass2 = new simpleClass();
objSimpleClass2.getabc(); //should return 1
objSimpleClass2.setabc(7);
objSimpleClass2.getabc() //now returns 7
You can’t instantiate literal object with new.
Instead of var simpleClassObj = new simpleClass(); you can do var simpleClassObj = simpleClass;
How can i call function using object of class?
myObj.function(); as simple as that