JS.Class updates

Yes, it only came out a couple days ago, but it’s a 0.9.x release, so it’s still being developed. If you downloaded JS.Class over the last couple days, I strongly recommend you upgrade to the latest version.

First off, it improves performance substantially over the initial release by inspecting method definitions to find out if they use this._super. If they don’t, they can be inserted straight into the class’ prototype without being wrapped in a _super-generating function. I believe Prototype and Base do something similar, though Inheritance seems not to.

Second, it fixes some subtle bugs to do with _super being reassigned when one _super-using function calls another _super-using function. This was a pretty basic oversight and was easily fixed.

Finally, it’s been much more thoroughly tested, the design has been tightened up, and instance method inheritance works better by using prototype chaining rather than brute-force method addition. That means you don’t have to use MyClass.method('name', func) if you don’t want to – you can just say MyClass.prototype.name = func and JavaScript’s own inheritance model will take care of the rest. (Although, if func uses this._super, you still need to add it using method.) Class method inheritance is still a bit of a pain, and you need to use MyClass.classMethod('name', func) if you want the method to be inherited.

Also, the docs have been augmented – I especially like the bit about module design and how closely you can mimic Ruby’s inheritance model using this library. As far as I know, this is the only JavaScript inheritance model in which you can use super without passing arguments back in, just like in Ruby.

If you’ve enjoyed this article, you might enjoy my recently published book JavaScript Testing Recipes. It’s full of simple techniques for writing modular, maintainable JavaScript apps in the browser and on the server.