How to import a module in es6 that itself needs to invoke/initialize its function/class before being imported
Asked Answered
U

2

10

I was wondering what is the best practice to import a module's function/class in another module that the module itself needs to invoke/initialize its own function/class before being imported into another module? I don't know if I could ask my question clearly enough! So let's put it in an example.

This is my module:

// myModule.js
class MyModule {
  constructor() {
    // do sth
  }
}

let myModule = new MyModule();

And this is how I like to import it in another module:

import MyModule from './myModule';

This actually works fine! But as you can see, in the myModule.js file I didn't export default my MyModule class because that's not the only thing that is happening in the myModule.js file! I'm also initializing the class after defining it... (I know that even if I have set my class as export default the initializing would still work fine while the module is imported somewhere else...)

So, without setting anything as exported inside of our module, or with setting the class as the export default, everything works fine when the module has been imported somewhere else... So far so good! But I'm looking for a best practice if there's one!

So here are my questions regarding such cases:

  1. Is it ok to import a module that doesn't have anything for export?
  2. Shall we set the class as the export default, although we are doing some more works outside of the class in the module (The initializing job that is happening after defining the class)?
  3. Or maybe is it good to do the initializing job in another function and then export both, the class and the function, and then invoke the function to do the initializing job in the imported module?

Thanks a lot everyone! I really appreciate any helps regarding this :)

Unknit answered 16/8, 2016 at 17:15 Comment(5)
"because that's not the only thing that is happening" should not keep you from (default) exporting anything.Foss
Do you need multiple instances of the class?Foss
I'm wondering what if I have a factory function (inside of it I am not only instantiating the class but also doing some other works) that I like it to be self invoked. How can I export that self invoking function?Unknit
Generally I'm wondering how can we export a self invoking function?Unknit
Drop the self-invocation, ES6 modules are self-contained anyway. Just execute your stuff in the module level scope, export the results.Foss
S
21

How about offering to import the class or the instance of it? Like:

// export class itself
export class MyModule {
  constructor() {
    // do sth
  }
}

// export instance of MyModule directly
export default new MyModule();

// export a factory function if you need more work to be done
// before the instance is created
export function myModuleFactory(...args) { // define e.g. arguments to be passed to constructor
  // ... do stuff
  const myModule = new MyModule(...args);
  // ... do more stuff
  return myModule;
}

So you could do:

// import instance
import instance from './myModule';
// or class
import { MyModule } from './myModule';
// or import factory
import { myModuleFactory } from './myModule';

What to do, depeneds on what you want to accomplish with your module. If you want your app use one shared instance of a MyModule class object, you’d export and import the instance like shown above. If you want to create multiple instances in different contexts, you’d export the class itself or a factory function to return a new instance.

To keep it even more clean, you’d keep the class in yet another separate file and import it into the module providing the factory/instantiation.

Update

To answer your first question: You can import modules that don’t have any export defined. The module will be loaded and its logic executed. The thing is that as long as it won’t change global variables (like window in web development) it won’t have any effect as everything inside the module happens within an isolated scope. As you may have guessed, having modules change global vars is far from best practice.

Shore answered 16/8, 2016 at 17:30 Comment(4)
Your solution may work for when we like to import the instance of the class... But what if we like to invoke the factory function and then export it? Is there any suggested way that I don't know about?Unknit
You mean invoking the factory function within the module we are importing from and export the created instance? Then you wouldn’t need a factory, as you could write all the stuff to be done right into the module itself and wouldn’t have to encapsulate it into another function (like //export instance of MyModule directly with some stuff done before). You wouldn’t be able to pass in any arguments though.Shore
No, of course we can easily export the instance of the class without any problems... No worries about that! Let's forget about the instatiation... Here is my question: How can I export a self invoking function?Unknit
export default function() { ... }(); But what sense would it make? It would be executed the time the module is imported as all the other code in the module. So it doesn’t make a difference if your code is within the function or outside. If you really need an isolated scope here, it is probably a hint that you should separate the code into a module of its own.Shore
U
-2

Thanks a lot guys for your answers. I really appreciate that. Actually I got really confused on the project that I'm working on, so maybe I couldn't express what I'm trying to do...

But anyway, I write my own answers to my questions, maybe someone else find them useful:

  1. It's always a good practice to have a export default or export for a module that we're going to write. Because every piece of code tends to have some results, right? So in a module, we should consider what we're going to achieve at the end and then export that. Export your outputs, the things that you expect your module to provide when it is going to be imported somewhere else.

  2. If your module is a single class, it's good to export default it. Otherwise, as I said in the first answer, it all depends in what you're going to achieve in your module and what is your results. Export all the results, utility functions, etc...

  3. You may like to do that too! But first think about your use case. As soon as the module is imported somewhere else, all the codes inside of it will be executed. So do whatever you like to do and then export the end results.

Unknit answered 18/8, 2016 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.