mardi 5 janvier 2016

JavaScript Independent Modules

I have several modules in my application and some global variables. One module may require some methods from other modules to work and also some global variables.

How can I organise my code so modules won't use methods from other modules or global variables?

//global variables needed in both of the modules.
var a, b;

//VIDEO MODULE
var VIDEO = (function() {

    //requires a method from USER MODULE:
    if(USER.has_liked_video()) {
        //do smth.
    }

    //requires some global variables

    return {}
})();

//USER MODULE
var USER = (function() {

    //requires a method from VIDEO MODULE:
    if(VIDEO.exists()) {
        //do smth.
    }

    //requires some global variables

    return {}
})();

Aucun commentaire:

Enregistrer un commentaire