I don't know which approach is better with ES6 modules and the revealing module pattern. Is the data / functionality from an ES6 module as private as an IIFE?
Should I just use *only ES6 modules, like so:
// Export file
export const test = () => {
console.log('Hello from test');
}
// Import file
import { test } from "./test.js";
test();
Or should I use both combined:
// Export file
export const revealingPattern = (function() {
function test() {
console.log('Hello from test');
}
return {
test
}
})();
// Import file
import { revealingPattern } from "./test.js";
revealingPattern.test();
The main purpose of the revealing module pattern is to keep data encapsulated, but the top level of an ES6 module is already private - variables defined in it do not leak to the global scope (unless you assign to the global object explicitly, like window.foo = 'foo'
).
So, in an ES6 module, there's not really any point to the revealing module pattern - feel free to define whatever you want on the top level, and it'll be scoped to the module (and only the module), and then you can explicitly export
whatever needs to be revealed (and nothing else will be undesirably revealed).