Hi,
Using jQuery, this is the format of most of the classes I write in JS:
$.Tabs = function(options){
var self = this;
this.defaults = {};
var cfg = $.extend(true, {}, this.defaults, options);
this.$elem = cfg.$elem;
function _init(){
var dontInit = false;
if (!cfg.$elem || !cfg.requiredProperty){
alert('missing params');
dontInit = true;
}
if (!dontInit){
self.$elem.bridge(self);
}
}
this.show = function(){};
this.hide = function(){};
_init();
}
The code above is just an example by the way.
My question is, how do you extrapolate common tasks/functionality from your classes, so every new class you write, you don't have to retype everything again?
I created a Class creation script based off of John Resig's Simple Javascript Inheritance pattern, which allows you to require certain parameters, bind event listeners for a pub/sub pattern, automatically combine defaults with options passed in, and it creates a bridge between the DOM element and the class instance (i.e $('elem').data('instanceNamespace', classInstance);)
But I'm uncomfortable with continuously using this class script to write my classes because it makes them less portable. But yet, I don't want all my classes to have so many similarities between them when it feels like those aspects can be abstracted.
Thoughts? Or how do you format your Javascript classes?
Thanks in advance.
Edit: Also, would anyone know the impact using a class creation script has on performance? I'm assuming the extra closures adds overhead but is it enough to make it not worthwhile to use?