Thanks to Google, people suddenly woke up to realize the true potential of Javascript. Before that a million others tried to convince the world that this language has got more than they can imagine, but that hardly helped! I remember this one instance when GMail was launched, and a popular web browser had to come out out with a quick patch in order to make it GMail compatible. Now, if you and I had a similar issue and approached the same people we would be given a hand book on browser standards and compatibilities and asked to learn to live in the 'real world'. Well, this world works on 'brand' capitalization - nothing can replace the power of a brand (except money of course)!
I have always enjoyed coding in Javascript for the last ten odd years. Of course, it took me a long time to realize the true potential of object oriented javascript! My professional work was more in the Java world for a long time. After doing a bit of freelancing using MS Visual Studio, Perl and a few other technologies, I started my regular career on a Java based proprietary framework called WebObjects (this, for those of you who don't know, was at least 5 years ahead of it's time - was more like a combination of Tapestry and Hibernate). After that I went on to work on developing a C library, and later on worked on Tapestry, Hibernate and Spring too. I really enjoy working on a wide variety of technologies, and always prefer to be a jack of all, rather than an expert in something. I believe that being involved in diverse technologies makes you a lot more creative while dealing with tricky situations.
Hmm, now this post is supposed to deal with Javascript, so why am I talking about what I did! Well, now, didn't I mention branding :p! Well, I just want to set the context right guys - I have been there, seen that, and I really know where Javascript stands in terms of churning out amazing code that brings out amazing results. So, if you are a programmer, and if you ever get a chance to deal with raw Javascript, don't think twice - accept it straight away! By the way I didn't use the word 'raw' just like that. Sometime soon I will elaborate on this.
Javascript is basically a 'prototype' based object oriented language. What this means is that you don't describe an object (as in, you don't write a class), but rather use a sample object to define a type. It's a loosely typed language and does not need you to declare types for variables. It can also implicitly convert types as needed. As in Java, Javascript has primitive types and objects. String, number and boolean constitute the primitive types.
Every object in Javascript behaves like a hashmap, and you can determine which property of an object needs to be accessed at runtime. This is a very cool feature, and the last I had dealt with something close to that in Java was while programming using one of the WebObjects frameworks called Enterprise Objects Framework (EOF, please sit up and take note, is like a great ancestor of Hibernate and many other contemporary ORM frameworks). EOF had this base class that enabled a similar functionality using Java's reflection API, and programmers in general wouldn't have to deal with all the complexity of reflection. It definitely helped in trimming down the number of lines while implementing complex business functionality.
Now lets discuss a bit about functions in Javascript. One amazing thing is that every function declared in Javascript is treated like any other object. This means you can actually throw your functions around your code, pass them to other functions, and hold references to them using variables! Hmm, if you ever worked on C, you should be drooling by now - yep, function pointers on steroids, that's what this is!! All those guys from the Java World - sorry guys we have nothing like that in Java (you can try it with reflection, but not really, not enough) - so just make sure you have enough strength to imagine beyond the ordinary to understand the power of function pointers.
Any function in Javascript can be used as a constructor. Now this might confuse beginners, but it's a very simple concept - whenever you call a function preceded by the new keyword, a new object is created and that function is executed in the new object's context (on the lines of a constructor).
Well, now for some not so great news. Javascript does not support function overloading. But I guess there's a valid reason. Javascript allows variable number of arguments to be passed to a function, which means you can pass more arguments than what's been declared for the function! So, then how can those other arguments be accessed? Well, simple, every function has an internal variable called 'arguments' which is like an array of arguments, and all arguments passed to a function can be accessed using 'arguments'.
Well, that kind of wraps up some key features. I will get back with more on Javascript pretty soon!
Kartik Reddy Thum
Senior Dev Engineer
No comments:
Post a Comment