Today I’ve released the first version of jQsimple-class, a small JavaScript class-declaration library. The reason I wrote it is that the usual way of building classes in JavaScript is frankly quite ugly, and inheritance is equally ugly. With jQsimple-class I’ve tried to make it as simple and intuitive as possible to write classes in JavaScript. The library itself is very small, and the syntax is simple. It’s meant to let you quickly declare a class, and easily extend others, and then get completely out of your way. It only exports a single variable/function, named jClass (Class is a reserved word in JavaScript, so I went for the next best thing). Using jClass, and methods on it, it is possible to build classes, virtual classes and extend classes.
jClass()
takes a single parameter, a JavaScript hash, where keys are method or attribute names, and the values are any valid JavaScript type. jClass.extend()
lets you build a class that extends one or more existing classes. jClass.virtual()
lets you construct a “virtual” class. That is to say, a class that can not be instantiated, but that can be extended by others.
Internally jQsimple-class uses some jQuery methods, but it does not depend upon jQuery to be used, a standalone version that bundles the parts it needs (not all of jQuery, and without exposing them to the public namespace) is available for applications that do not use jQuery. I have written an extensive testsuite for jQsimple-class to make sure that things work as they should, and it works across all modern browsers.
For more examples and the full API, see the jQsimple-class documentation. jQsimple-class version 0.1 is available for download now. Minified it is only 1.5K (or 9K for the standalone version). Any feedback is welcome, feel free to do so in the comments, or, if you find a bug, on the bugtracker.