Tuesday, June 17, 2008

jsClass - Object Oriented Javascript


jsClass is an extensible Javascript Base Class that enables you to do clean Object Oriented Programming (OOP) with Javascript.

Why Object Oriented Programming ?

OOP is one of the multiple approaches you can take to structure your code.
It doesn't fit any situation, it's proven to be an outstanding approach to handle large/complex applications.
It will help you keep your code clear, extensible, decoupled and maintainable.
For further information on OOP, just check some of the links on this article.

Extensions and Design Patterns

jsClass accepts extensions, that is, custom modifications that you can attach to classes.
Some are tightly related to Design Patterns.
These are general reusable solutions to commonly occurring problems.

Each extension is very well documented on the code, specifying the different ways yo use them. Also, check the demo to see each in action.
This is the list of built-in extensions:
  • abstract
    Create abstract classes that cannot be instantiated without extending.
  • extend
    Use inheritance, make child classes extend base classes.
  • getset
    Generate getters and setters for private members of your class.
  • implement
    Create Interfaces that define methods that need to be defined, in order to implement them.
  • members
    Add members to the instances of your class. It's a shortcut for SomeClass.members(...).
  • module
    Save your class into a Module. If required, all the namespace is created before saving the class.
  • properties
    Generate properties to control the access to private members of your class.
  • singleton
    Create Singleton classes that can only be instantiated once.
  • statics
    Add static methods or data to your class. It's a shortcut for SomeClass.statics(...).

How to use ?

The best way is to see it in action. Check the demo (link below) to see different uses of jsClass and its extensions.



dvdrtrgn said...

You're doing fantastic work for jQuery. Kudos!

Anonymous said...

I love the libraries you've created. In the jsClass scripts, how do you create private variables in classes.

Ariel Flesler said...

Hi BlueTopBrown

You can't for now. I have a nice update (RC2) on the trunk that I haven't released yet.

You normally don't worry about private vars on javascript, you simply don't document them or say they're private.

Anonymous said...

This library is wonderful!
I've a question about "extend" extension. Is it possible to extend many bases ?

Ariel Flesler said...


No, this framework doesn't allow multiple inheritance.
It is mostly based on the Java/C# Object system.

I have a much newer version on trunk, I'll try to release it asap.

Anonymous said...

Love this! Even a retrogrouch like myself can avoid mastering prototyical inheritance for yet a while longer. ;o)~

Any guestimate on when you might release rc2 trunk? No rush, just wondering what goodies are in it.

Dave M.

Anonymous said...

Silly question: Im trying to test run some of your OO sample code using jsClass as server side code on IIS, but Im running into 'object or property does not exist' errors.

Do I need to declare some kind of global object that jsClass is expecting or assuming is available? It doesnt seem to like when it's not running in a browser environment. (or maybe it just hates IIS as much as I do! [grin])

Dave M

Ariel Flesler said...

Not sure, do you have the file and line number ?
Abotu RC2... I kinda stopped deving this, but I'll try to get back on track :)

Anonymous said...

After noodling and mucking around the msdn site, it appears the verson of jscript running on my IIS6 server (jscript.dll) is 5.6.8832.

From the MS dox, that version is only equivilant to javascript1.5 which doesnt support generics. (among lots of other things) doh!

So Im 99% sure that's why im getting the "property does not exist" on all calls to the Class.copy function.

To fix it, looks like im going to have to kluge together a way to upgrade the version of js running on the server. and if i know MS....

....that's probably not an option w/o upgrading to their latest web$erver ver$ion.


you said: "botu RC2... I kinda stopped deving this, but I'll try to get back on track"

I wouldnt worry too much, I just wanted a peek at it, as I loved the way jsClass lib was set up and named. Basically, if I can't upgrade my server to run at least javascript1.6 it's all for naught anywho, as I don't do much clientside JS.

Tnx again for the lib. really nice stuff.
Dave M