1

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
3
  • 1
    The console output happens because both module_1.prototype and module_2.prototype refer to the same object, so you overwrite the table property. Commented Feb 20, 2015 at 11:59
  • I know.. But how to solve it?! Commented Feb 20, 2015 at 12:05
  • It is a bit unclear to me what you want to do there. If you want to create different objects with the same constructor, you can pass all the values (e.g. table, module_name) as parameters. Or you can create new constructors which inherit from Delegate and change their prototypes. Commented Feb 20, 2015 at 12:10

1 Answer 1

2

Sounds like you want to use inheritance with two extra constructors module_1 and module_2 that both call the Delegate:

function Delegate(main, location) {
    this.module = null;
    this.init = function() { // you should do initialisation stuff directly in the
                             // constructor, not an `init` method
        this.module = new App[this.module_name](main, location, this.table);
        this.module.init();
    };
}
Delegate.prototype.test = function(){
    this.module.test();
};

function Module_1(main, location) {
    Delegate.call(this, main, location);
}
Module_1.prototype = Object.create(Delegate.prototype);
Module_1.prototype.table = 'debtor_group';
Module_1.prototype.module_name = 'module_group';


function Module_2(main, location) {
    Delegate.call(this, main, location);
}
Module_2.prototype = Object.create(Delegate.prototype);
Module_2.prototype.table = 'creditor_group';
Module_2.prototype.module_name = 'module_group';
Sign up to request clarification or add additional context in comments.

1 Comment

Your solutions is just what I was looking for.. But what does this line do? I can escape the line and the code still works Module_1.prototype = Object.create(Delegate.prototype);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.