I like working with JavaScript classes, but I really don’t like the way I have to contruct them. I have been researching a way to clean up this process a few months back, and after reading about how base2 and Prototype.js do it, I stumbled upon John Resig’s Simple JavaScript Inheritance method. It’s simply brilliant, and so easy to turn into a proper jQuery plug-in.
So I can’t claim any ownership on this at all. It’s simply Resig’s code wrapped in a jQuery plug-in so that it can be called like this:
MyApp = $.Class.extend({
init : function() {
}
});
One thing I added though, is a way to bind a specific scope to a function call. I have seen this being used in Prototype.js, and it comes in really handy. For instance you can do this and keep your instance’s scope on the function that’s called after the success response:
$.ajax({
url : "/path/to/processor",
type : "POST",
success : this.parse_results.bind(this)
});
You can get the plug-in from its repository on GitHub.
© Copyright 2001-2010 Taylan Pince. All rights reserved.