You could use something like the MooTools Class which will be slower than your method with the 'hanging' code. This is mostly due to the fact that the MooTools Class has overhead associated with their 'Native' structure. A lot of stuff you may not, and will likely not, need. It sounds like you want something straight-to-the-point.
You could do something like:
var Klass = function(attrs) {
var key, newClass;
attrs = attrs || {};
newClass = function() {
if(this.init) {
this.init.apply(this, Array.prototype.slice.call(arguments));
}
};
for(key in attrs) {
newClass.prototype[key] = attrs[key]
}
return newClass;
};
Then, you could do something like:
var Person = new Klass({
init: function(name, bg) {
this.name = name;
document.getElementById('container').styles.background = bg;
},
speak: function() {
'I am ' + this.name;
}
});
var tim = new Person('tim', 'blue');
// background now blue
tim.speak();
// 'I am tim'
var jane = new Person('jane', 'purple');
// background now purple
jane.speak();
// 'I am jane'
So, this code will fire off the init() when you make a new instance of a class. Passing it whatever arguments. In which you can put your 'hanging' code. Node that, due to the way prototypes work, this does not allow you to make a new instance of a new instance. And you would need to change it further to allow for things like building off of a parent class. But this is a pretty simple way of building a class structure.
This should work fine for what you want. If you were to come across something it "can't do" there will certainly be a way to correct that. You may just have to finagle the code. As I said in the previous paragraph. There will be a little overhead since a class is basically a function being returned, which you then make a new instance of. But nothing too significant. As for best practices, I would say it is not bad. There are a number of JS libraries that have quite extensive class structures in place for you to use. And a lot of people do use classes. But there are and will be arguments over the use of them since they are not real native JS types.
thisObject = this, supposed to accomplish?var thisObject = this;. Poor sample code!