Search code examples
javascriptappceleratortitanium-mobile

can we have more than one module.exports statements in one js file


I am newbie to javascript.I am developing mobile apps using Titanium studio. My question is regarding javascript which is as follows.

I have one file js file named commonUi.js in this file I am defining common UI components which I can use in different windows.

Now I am defining two object function inside commonUi.js which are as follows.

function component1(){

}

function component2(){

}

Now My core question is can I write following two statements inside my commonUi.js file

  1. module.exports = component1;
  2. module.exports = component2;

Any help greatly appreciated.


Solution

  • You can only have one module.exports = in your module, but you can do something like this to achieve what you want:

    var CommonUi = function() {
    
        var component1 = function() {
    
        }
    
        var component2 = function() {
    
        }
    
        return {
            component1: component1
           ,component2: component2
        }
    }();
    
    module.exports = CommonUi;
    

    And then you can just use it like this: commonUi.component1();