Search code examples
javascriptfunctionmeteormodule-pattern

Meteor JS global function


using Meteor.js I want to use a global function which contains other functions :

BIG = function (){

     this.init = function ()
     {
        //do something
     }

     this.addSomething = function (param1, param2)
     {
        //do something else
     }

}

Now im calling this functions like :

 BIG.init();
 BIG.addSomething(param1, param2);

But this is not working, the console print "BIG.init is not a function". When i type "BIG" in my console it print back "function BIG()" which means Meteor recognize that BIG is a function but does'nt recognize the subfunctions inside BIG.

Any help on how can i achieve this ?

Thanks.


Solution

  • Syntax, should be this (according to your current syntax):

    new BIG().init();
    new BIG().addSomething();
    

    You see, you need construct a new BIG instance using the new keyword to properly set the this context inside of BIG, then it will return it's methods for you to execute.

    But I doubt that's actually what you are trying to do. Are you sure you don't just want to declare an object literal like below:

    BIG = {
    
         init: function ()
         {
            //do something
         }
    
         addSomething: function (param1, param2)
         {
            //do something else
         }
    
    }
    BIG.init();
    BIG.addSomething();