vendredi 23 août 2019

Instantiating module object on module's file vs. instantiating module object on file that uses it

Are there any advantages/disadvantages to instantiate a module object on the module's file instead of instantiaing the module object on the file that uses it?

Example of Instantiating module on the module's file:

module.js

const someModule = (function() {
  const doSomething = function(id) {
    console.log("doing something");
  };

  return {
    doSomething: doSomething
  };
})();

export someModule;

main.js

import { someModule } from "./module";
someModule.doSomething();

Example of instantiating module on the file that uses it:

module.js

const someModule = function() {
  const doSomething = function(id) {
    console.log("doing something");
  };

  return {
    doSomething: doSomething
  };
};

export someModule;

main.js

import { someModule } from "./module";
someModuleInstance = new someModule();
someMosomeModuleInstance.doSomething();

The first example only has "someModule"(from the import statement) on the main file's global scope while the second example has both "someModule" and its instance on the global scope. So is the first example better?

Aucun commentaire:

Enregistrer un commentaire