Search code examples
node.jsrequire

centralize the repeated code into a single file


language nodejs

I have a bulky code, which is repeated the same way in many files: file1.js, file2.js, file3.js, ..., each file has a different instance of the same class, the only difference is the object instance,

my situation resembles this pseudo-code

    fileClass1.js
    MyClass1{
        constructor(name) {
            this.name = name;
        }    
        printName(from){
            console.log("Hello "+this.name+" from "+from)
        }
    }

    file1.js
        var obj = obj1 = new MyClass1("name1")

        //call MyClass1::printName()
        obj.printName("foo")
        obj.printName("bar")

        //methods definition 
        obj.method2 = function(){}
        obj.method3 = function(){}

    file2.js
        var obj = obj2 = new MyClass1("name2")

        //call MyClass1::printName()
        obj.printName("foo")
        obj.printName("bar")

        //methods definition 
        obj.method2(){}
        obj.method3(){}

    file3.js
        var obj = obj3 = new MyClass1("name3")

        //call MyClass1::printName()
        obj.printName("foo")
        obj.printName("bar")

        //methods definition 
        obj.method2(){}
        obj.method3(){}

    . . .

the result I would like to obtain is shown below: an include.js file that contains the code that call the method, and the definition of methods, or only one of the two if both are not possible

    fileClass1.js
    MyClass1{
        printName(){
            console.log("method1 Hello!")
        }
    }

    file1.js
        var obj = obj1 = new MyClass1("name1")
        require(include.js)


    file2.js
        var obj = obj2 = new MyClass1("name1")
        require(include.js)

    file3.js
        var obj = obj3 = new MyClass1("name1")
        require(include.js)

    . . .

    include.js
        //call MyClass1::printName()
        obj.printName("foo")
        obj.printName("bar")

        //methods definition 
        obj.method2(){}
        obj.method3(){}

Solution

  • Requiring a JS file doesn't mean that it has to run straight away, you can create a function in include.js and call that function later, instead of its code executing straight away.

    Read more about JS Modules

    // include.js
    module.exports = function(obj) {
        obj.printName("foo")
        //call MyClass1::printName()
        obj.printName("foo")
        obj.printName("bar")
    
        //methods definition 
        obj.method2(){}
        obj.method3(){}
    }
    

    From now on, when you require('include.js'), it will return a function which can be stored in a variable

    // file1.js
    var obj = obj1 = new MyClass1("name1")
    var doStuff = require('include.js')
    doStuff(obj1)
    
    
    // file2.js
    var obj = obj2 = new MyClass1("name1")
    var doStuff = require('include.js')
    doStuff(obj2)
    
    // file3.js
    var obj = obj3 = new MyClass1("name1")
    var doStuff = require('include.js')
    doStuff(obj3)