I've implemented the following code which allows me to bolt modules of code on to my core library via the prototype chain. It's quite prescriptive so any user creating a module has to create an init method and also has to define a queue method:
(working fiddle: http://jsfiddle.net/wG9Pv/)
//Constructor
test = function() {
this.tester = 'The instance was successfully created';
};
//A core method
test.prototype.amethod = function() {
};
//Extend method which facilitates the module system
test.extend = function(name,plugin) {
test.prototype[name] = plugin.init;
//later do something with the queue method that's been fed in via the 'plugin' object
};
//Add a module
test.extend('method1', {
init: function(val) {
console.log(this.tester);
},
queue: function() {
},
render: function() {
}
});
//add another module
test.extend('method2',{
init: function() {
console.log('Init of the plugin has been fired');
},
draw : function() {}
});
var test = new test();
test.method1('boom!');
test.method2();
Now this all works well but is this a good way of achieving it?