Object Oriented JavaScript
Javascript 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.
Discuss This Article
|
|
6 Responses to “Object Oriented JavaScript”
-
pingback:
Posted: Mar 10th, 2006 at 10:06 amBorkWeb » Blog Archive » Ajax; Templating; and the Separation of Layout and Logic 1 -
Krzysztof
Posted: Mar 17th, 2006 at 5:25 amReply to this comment.something has been spoiled with your decorative links at the end of your blog - I had to ‘view source’ to retrieve them!
2 -
Jeroen
Posted: Jun 7th, 2006 at 6:13 amReply to this comment.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/
4 -
pingback:
Posted: Jun 28th, 2006 at 6:17 amBorkWeb » Geeks, Nerds and Dweebs Reply to this comment.[...] 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 :) [...]
5 -
UselessPickles
Posted: Oct 27th, 2006 at 1:01 pmReply to this comment.I’d like to challenge Jeroen with my object-oriented framework at http://www.uselesspickles.com/blog/the-class-library/ :).
6



[...] 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). [...]