Sunday, 6 January 2008

Javascript Objects

I remember a long time ago in a job interview, saying that there was no such thing as javascript objects. This was when the notion of javascript objects was introduced on very hoky browsers like Netscape and IE. It certainly wasn't cross-browser friendly then and it looks like not a lot has changed there. I still think Javascript objects and classes are immature. Looking at articles on Javascript objects, there are number of different notations that could give average developer a nightmare. For instance this oop javascript guide gives a thorough overview how to construct a javascript class properly. However, I do not like the idea of having to explicitly utilise such functions as makeConstructor, defineMethod, defineType, instantialize, getClassName. That said, the article is spot on its assessment of Javascript OOP...

"What you may say is 'Hey, the syntax is too complicated! Writing those syntaxes will frustrate me ...'
Actually, JavaScript is not an Object Oriented Programming language. But we create context so we can do OOP. And once the syntax is too complicated, we can simplify them. But I consider borrowing the existed mature Java language would be a better choice. That is, we can write in the style of Java, which is sure an OOP style language, and then we converts the Java codes into JavaScript codes, which is somewhat syntactic complicated. And then we have a great thing."

Fortunately, there are more simpler articles that demonstrates ways of creating Javascript objects, howtocreate, mozilla and mckoss still use the newly created class' prototype object. The only benefit I see, it gives you a chance to define functions and attributes for later and also can separate functions and attributes leaving the main class constructor. However, if you are comparing against natural OOP language like Java, C++ and C#, you usually do not have to define these functions and objects explicitly and you can incorporate methods and attributes into your classes (that you would normally see in naural OOP languages) as demonstrated in these even simpler articles from daaq and asp-ajax.

Also stumbled some interesting article on using JSON objects from hunlock and some useful javascript inheritance techniques in prototypejs.

No comments: