Object Oriented JavaScript

javascript.gifJavascript has invaded my life. And I like it.

I have developed web applications for quite some time, writing Javascript off an often unnecessary tool that simply added frills. Over the course of this year – with the birth of the Ajax hype – I realize my error. I began inserting javascript all over hell’s half acre and raving about its sweetness. I’ve grown in a year.

As I develop more javascript-rich web applications for my amusement and my job, I am constantly seeking better ways to structure both data and code. It wasn’t until a month ago that I really began to dig into the Prototype and Script.aculo.us code (rather than simply using it) and began to grow aware of what was actually going on. Object Oriented Javascript.

I think it is important to note (so I don’t look like a complete nitwit) that I was aware that Javascript had this capability a couple of months back before I picked up and started using the afore mentioned Javascript libraries…I just had no drive to learn it and spot where it was being used. I have seen the light and it is good.

What are its benefits? Well, pretty much the same applies to Javascript that applies to any other programming language…OOP helps increase organization and reduce un-needed redundancy. And best of all? It helps prevent dual-declared functions and variables. That is the reason I chose to begin my quest for OO-JS familiarity.

I had gone hunting at a few other sites that claimed to be good references (obviously) of OO-JS, but they lacked the topic of inheritance, which is the real strength of any OOP Language. The most helpful article I found on the topic was at WebReference. They discuss the use of prototype (Not the Javascript Library). Here’s a quick excerpt, but I’d suggest you read the article for yourself!

Excerpt from WebReference

function Person() {
      this.name = "Rob Roberson";
      this.age = 31;
}

function Employee() {
      this.dept = "HR";
      this.manager = "John Johnson";
}

Employee.prototype = new Person();

var Ken = new Employee();

Ken is in the prototype chain of Employee, Person, and Object.

Check it out. Use it. You’ll like it. You’re applications will like it. Oh, and it may prove useful to check out some Javascript Coding Conventions.


Comments

7 responses to “Object Oriented JavaScript”

  1. […] I have often mentioned my process of expanding my proficiency of Ajax. Through my journey I have made a number of wrong turns and hit my share of stumbling blocks. All of that has been a learning experience and I’m learning still. I began fiddling with XMLHttpRequest as many do – blissfully ignorant of the many frameworks that exist to make Ajax super easy. My code was bloated with some neat…’features’ (pronounced: bugs). […]

  2. Krzysztof Avatar
    Krzysztof

    something has been spoiled with your decorative links at the end of your blog – I had to ‘view source’ to retrieve them!

  3. Should be all fixed now :) Thanks for the heads up!

  4. For people that are interested in a Java/C#-style OOP in JavaScript, I have created a small shareware library. You can download it at http://software.antrix.nl/

  5. […] I’m proud of my Geekiness. I’ll be the first to call myself a geek, but what really irks me is when I proclaim myself as one, my family immediately replies with a “nahhh…you’re not a geek,” in a comforting tone. I’ve worked hard to be obsessive over random things (Star Wars, Warcrack, Programming, etc)…let me have my cake and eat it too :) […]

  6. I’d like to challenge Jeroen with my object-oriented framework at http://www.uselesspickles.com/blog/the-class-library/ :).

  7. Usually I’d suggest jQuery for everything, however in terms of OOP it does not provide much support. For that reason, we have adopted a small framework inspired by Extjs4, that works alongside with jQuery, but provides strong OOP support. Take a look at http://code.google.com/p/core-framework/