How to reuse the same constructor function in multiple objects?
Here creditor_group is constructed in both objects.. How to duplicate the Delegate function!?
http://jsfiddle.net/q2nxuhyc/2/
code
var App = {};
App.module_group = function(main, location, table){
this.init = function(){
console.log('init: '+table+' args: '+main+', '+location);
};
this.test = function(){
console.log('test: '+table);
};
};
function Delegate(main, location){
this.table;
this.module_name;
var module;
this.init = function(){
module = new App[this.module_name](main, location, this.table);
module.init();
return module;
};
this.test = function(){
module.test();
};
}
var module_1 = Delegate;
module_1.prototype.table = 'debtor_group';
module_1.prototype.module_name = 'module_group';
var module_2 = Delegate;
module_2.prototype.table = 'creditor_group';
module_2.prototype.module_name = 'module_group';
// This part where the objects are constructed is done in another scope
var m_1 = new module_1('main', 'location');
m_1.init();
m_1.test();
var m_2 = new module_2('main', 'location');
m_2.init();
m_2.test();
console
init: creditor_group args: main, location
test: creditor_group
init: creditor_group args: main, location
test: creditor_group
module_1.prototypeandmodule_2.prototyperefer to the same object, so you overwrite the table property.