0

I am loading separate .js files into my html page. These files use anonymous functions so that the global scope is not filled with global variables. For example: this is my 'utils.js' file:

(function(window){
   var Utils = {
       createURL: function() {
           console.log("creating url");
       }
   };
})(window);

My question: if I have several of these .js files, how can they reference each other without using the global scope? As it stands, I can only call this function from within the same utils.js file:

var myUtils = new Utils();
4
  • Have a look at require.js. Commented Jul 3, 2013 at 12:43
  • You sure you are not getting an error Utils is not a constructor with the code you have got? Commented Jul 3, 2013 at 12:44
  • You can use namespaces. Look here for details : kenneth-truyers.net/2013/04/27/… Commented Jul 3, 2013 at 12:45
  • You always will need to use the global scope somehow, otherwise you cannot set up any connection between the files. Commented Jul 3, 2013 at 12:53

4 Answers 4

2

You can create global modules and expose only the needed functions like this...

// In some JS file
var moduleX = (function(window){
   var Utils = function(){

   };

   return {
       "Utils" : Utils
   };
})(window);

// In a some other JS file
var util = new moduleX.Utils();

Btw, you cannot instantiate a JSON object(Utils in your case). So please change that to a function.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use a javascript loader like

If you don't want to use a loader, maybe you can reduce the global usage to the minimun using a design pattern like the mediator or module.

Comments

1
(function(window){
   var app = {};
    app.Utils = {
        createUrl:function(){}
    };
    window.app = app;
})(window);

and then you can work with the app like this :

app.Utils.createUrl();

Comments

1

If you don't want to use a third party tool, you can wrap everything into one module by using the following pattern:

//file1
var myModule = (function(window, that){

    var myVar = 1;

    that.myMethod1 = function(){
        console.log("i am module 1");
    };

    return that;
}(window, myModule || {}));

//file2
var myModule = (function(window, that){

    that.myMethod2 = function(){
        console.log(typeof myVar); //undefined
        that.myMethod1();   //i am module 1
    };

    return that;
}(window, myModule || {}));

This will ensure that you only have one single global variable. But, you will of course not be able to access local variables of another file.

1 Comment

thanks. I've written it slightly differently, like this: window.MAINAPP = {}; and then, I attach every anonymous function to MAINAPP, like this: MAINAPP.myModule = myModule;

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.