Whatever I am going to write is here is heavily borrowed from these 2 sources ->
The book ( have a look at the review for it ) -> http://www.i-marco.nl/weblog/archive/2008/08/22/objectoriented_javascript_by_s
And the blog -> link_
I feel java scripts are the most ignored and hated of all the things in a web application. When we write an application a lot of thought goes on the design of the application ( barring js ) and the database. The js developement remains fairly adhoc. And the result is we often turn our application into a monolith with a lot of java script codes , which slows down our page loads, and redundant calls which turns into a support person’s nightmare.
I have been reading about java scripts for the past few days. And found a couple of new things about it ( well new to me ). It looks quite cool …
I would like to touch upon some basic OO features that js provides , which looks pretty cool and can be used in web pages. So the very first thing , in java script is that it is object driven rather than class driven. It’s a bit hard to understand but what my understanding is , js does’nt know anything about classes. All it knows is about objects. It’s a bit weird specially for guys like me coming from the java background.
So how does an object looks like ->
To start with you need to create a function like this ( ideally these are known as constructor functions .. you can create objects in other ways too but this I guess is the simplest , widely used , reusable one .. )
function Animal(name) {
this.name=name;
this.getName = function() { return this.name; }
}
Now to create an animal object and use it’s method , use this. Voila it’s simple is’nt it ->
>>>var dog = new Animal(‘Dog’);
>>>dog.getName();
“Dog”
I am not sure how you test out the js but you can use firebug console in firefox it’s really cool.
Ok so now we have the dog object. Interesting to know that the arrays that we use in js often are actually Array objects that js creates for us. So when we say ->
var arr = ['1','2','3','4'];
It actually creates an array object called arr. So we can use the array object methods on it like a.push(’9′) to add an element to the array or a.pop() to get an element out of it.
So far so good, now what if I try to implement inheritance with java script. Is that possible ?
Every object has a secret prototype property. Prototype is a property that gets called immediately as the object is created. You can add methods and variables to this prototype object.
This prototype property is also consulted when you try to access a property that does not exist in the current object.
Ok so let’s look at the following example ( borrowed from the book OO Java script by Stefanov ) ->
//creating an anonymous object and assigning it to monkey variable
var monkey = {
feeds: ‘bananas’,
breathes:’air’
};
function Human() {}
Human.prototype = monkey;
Ok so we create a developer object now and give it some properties ->
var developer = new Human();
developer.feeds= ‘pizza’;
Now when we try looking up feeds , it is a property of the developer object so,
>>>developer.feeds
“pizza”
However when we say developer.feeds since it is not present in the developer object as such it looks up the prototype link to get the appropriate property in the prototype chain. The result ->
>>>developer.feeds
“pizza”
Using this you can augment the built in functions. The following is an example ( it is always better to check if the function is present or not , this ensures if the function is written in later js releases we are not overwriting it )->
if(! String.prototype.reverse) {
String.prototype.reverse= function() { return //the actual logic };
}
I plan to add the function contains to an array that will return a boolean true if the object is present in the array.Well all these was just the startup, of course for further reading you can consult any of the sources mentioned above ( or go a google search on it
)…