0

I'm taking some courses over at Udemy so I can get a better understanding of JavaScript. In the course I am taking, I've been introduced to ES6 blocks for data privacy. Up to this point I was using IIFEs for this purpose. I also like that IIFEs can be used to define modules for larger projects and would like to use ES6 blocks instead if I can, but I cant figure out how to access the other blocks from from another one.

Lets say we have a JS app we written in ES5:

 var ES5DataModule = (function() {
//some code...
})();

var ES5UIModule = (function() {
//some code...
)}();

var ES5Controller = (function(data, ui) {
//some code...
)}(ES5DataModule , ES5UIModule);

As you can see I have given the ES5Controller access to the other two modules in the example above. However in ES6, we simply use {...} to define a module. the variables in it are block specific. My questions here are twofold:

  1. Can we still return variables so they can be accessed by other modules using ES6 blocks?

  2. How do we give access to other modules with the block setup? I'm hoping it's as simple as including a let declaration. EG:

let ES6DataModule = {
//some code...
};

let ES6UIModule = {
//some code...
};

let ES6Controller  = {
//some code i want to have access to the other 2 modules, but have no idea how to make this happen...
};

I tried posting this as a question on the Q&A forums for Udemy. Unfortunately, no one responded to the question after a week, so I can surmise the answer is either obvious or simply no one is there. Either way, I'll risk rep by turning to the community.

  • 4
    "*in ES6, we simply use {...} to define a module*" - uh, no, that doesn't work. You should be getting a syntax error from that. – Bergi Apr 29 '20 at 15:36
  • see? that's all I needed to know right there. – Justin Egan Apr 29 '20 at 15:39
  • 2
    Related, if not duplicate: [Namespacing with IIFE in ES6?](https://stackoverflow.com/q/32746615/1048572) and [Will const and let make the IIFE pattern unnecessary?](https://stackoverflow.com/q/33534485/1048572) – Bergi Apr 29 '20 at 15:39
  • The second link answers the question definitively. Unfortunately, I didn't find that one while searching. – Justin Egan Apr 29 '20 at 15:50

0 Answers0