OOP (Object Oriented Programming / Pain) with JavaScript

As web 2.0 applications typically make use of heavy javascript to provide better usability, including the use of the AJAX feature, the client-side scripting code is going to bloat. Managing this code as a bunch of variables and functions soon will become unmanageable and hence it’s better to start making use of OOP features of JavaScript. Few other reasons in addition to the code bloat for using OOP is to reduce the global space proliferation and potential name conflict and also when providing a library for others to create mashups of your service, like the popular Google Maps, abstracting the entire API through a set of objects is a better solution.

Anyway, I have been just experimenting with the OOP concepts and learned something new. Apparently, using this.variable makes the variable a public variable while declaring it as “var variable” makes it a private variable. I don’t have any problem with this. But, what I am not happy with is, that the public variable should always be accessed as this.variable within the other member functions instead of accessing them directly as variable. The private variables defined using var however, can be accessed in this manner. In Java for example, irrespective of whether public, protected or private is used to define a member variable, it can always be accessed within a member function without qualifying it with the this reference. However, this is not the case with JavaScript and that’s a bit of a pain and been one of the source of my bugs.

Advertisements

Leave a comment

Filed under javascript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s