Tuesday, October 30, 2007

The Enhanting World of Java Script

I got a call from a person today and he asked me some questions. I did not know the what he wanted to know about me or what his final goal was( may be he wanted to intimidate me). It was a short interview and he concentrated on asking me if I knew this and that. The first thing he asked me was if I knew Java Script. He wanted me rate myself in Java Script. Not forgetting what a great 'language' Java Script I said I stood at 3 out of 5. This made me write some thing I know about Java Script thus justifying the 3 marks.

Java Script is an interpreted language and it could run on any application which has the Java Script interpreter in it. Major browsers, Adobe Acrobat Reader are some examples. What Java Script primarily does is facilitate the user to run scripts with in the application. In the case of browsers its accessibility of the DOM (Document Object Model) makes it powerful. It is not necessary that the browser that you use has all those Java Script features required. It is not necessary that the Java Script code is even executed at you end (security reasons). To cope up Fault Tolerant Systems may be used by those who create the actual page. It could access parts of the shown web page and change its attributes dynamically. That's the whole deal. There are many libraries that make life simple for you along with providing those nifty user interfaces and awesome effects.

Its a weakly typed, prototype based language. Prototype is an object that can be cloned to produce another object. The concept of classes is different from the so called prototype programming and the attribute can be dynamically changed at run time too. Every function created has an object called prototype and addition of new attributes can be done using this Object. That is you are dynamically changing the attribute of an Object so that it could be used for further processing in the rest of the script.

function circle(){
}
circle.prototype.pi=3.14159;

function alertmessage(){
alert(this.pi);
}
circle.prototype.alertpi=alertmessage;

Now all those circle object that you create will have the alertpi function and you could call the function. We could even extend the normal defined Objects (like String) and it will be reflected through out the whole code. Another way to code this would be -

circle = {}
circle.pi = 3.14159;
function alertmessage(){
alert(this.pi);
}
circle.alertpi=alertmessage;
circle.alertpi();

Declaring stuff inside the curly braces is as simple as var a = { pi : 3.14159 }. This is what essentially JSON delivers too. The essential idea of associative arrays also makes a difference with Java Script. ie a.x and a["x"] are the same. Eg.

circle = {}
circle.pi = 3.14159;
function alertmessage(){
alert(this["pi"]);
}
circle.alertpi=alertmessage;
circle.alertpi();

There are private members and public members in Java Script and they can be quite useful.

function aa(a){
this.p=a;
var secret =10;

function pp(){
alert(secret);
}

this.service = pp;
}

function ss(){
alert(this.secret );
}

ll = new aa(10);
ll.ss = ss;
ll.ss();
ll.service();

Here secret is private, p is public, and service is privileged (access to private and public stuff). Calling ll.pp() will give an error while calling ll.service() will give 10. Even calling ss will give you undefined.

CSS along with Java Script provide a great deal of WEB 2.0 'ism' to any web application. They are very frequently used to get the AJAX functionality so that the user stays in the same page even though the details are updated in different parts of the page. The accessibility to the DOM object of Java Script is made use of to update the parts of the page dynamically. The main Object responsible for making AJAX possible is XMLHttpRequest (XHR). Due to the differences between the browsers and their rendering techniques the initialization of this kind of object differs (new ActiveXObject for microsoft and new XMLHttpRequest for all the others ). But once it has been instantiated the properties( onreadystatechange, readyState etc) becomes visible. We could use the POST or GET methods to pass data and the object finally will have the resultant XML as the responseXML / responseText. We then use this text to load it into the actual object in the DOM using the innerHtml method (which is present of all objects in the DOM).

Now for the bare essentials core Java Script is fine. But when you need more functionalities to be done in your site then the pain of structuring your whole Java Script becomes a hurdle. To facilitate this a lot of libraries are in place now. The primary one being prototype. Prototype has many added functionalities like the easy access of the DOM Objects , nice AJAX manipulation, JSON support and some nifty Object oriented features (creation of classes ). Jquery is another library that is quite useful. Another Java Script library which beautifies your web application along with adding the necessary functionalities is the DOJO toolkit. Many other toolkits like Script-aculo-us and MooFX make things easier for the programmer.

Now to understand at least some of these functionalities ( I know I am bad at writing), we primarily need to see some source code. When I see some features that intimidate me in any web application (done by Java Script of course) what I do is see the source code and try to manipulate the code. As Fire Fox is my favorite browser I use Fire Bug for the debugging, viewing and manipulating purposes. It is a great tool and is a must have among all those Fire Fox plug ins.

Even though Java Script makes your life easier by trying to provide up to date information, it has many downsides to it. It may make the loading slow sometimes( as I have faced ). The security concern is a major hiccup for Java Script. I would never pass authentication information of any kind using Java Script as it could be easily replicated. Other major problem is the eval function which evaluates an expression given (converting between different objects).

One of the people I really look up to regarding Java Script is Douglas Crockford (JSON). He has a lot of videos which describe the Java Script language and all its functionalities. There are many tutorials that you will find in the net that will make you a better programmer. Don't forget to try out the Yahoo UI when you are comfortable.

As of Java 6, they have introduced Java Script support in their java.script package which is said to make use of the Mozilla Rhino engine( A Java based engine). I hope I could try to implement some of those neat features that they say they have.

I am still understanding what Java Script is and if you find some errors in my writing then please comment on this.

No comments: