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 bined:
// Export file
export const revealingPattern = (function() {
function test() {
console.log('Hello from test');
}
return {
test
}
})();
// Import file
import { revealingPattern } from "./test.js";
revealingPattern.test();
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 bined:
// Export file
export const revealingPattern = (function() {
function test() {
console.log('Hello from test');
}
return {
test
}
})();
// Import file
import { revealingPattern } from "./test.js";
revealingPattern.test();
Share
Improve this question
edited Jul 25, 2019 at 8:43
jonrsharpe
122k30 gold badges268 silver badges475 bronze badges
asked Jul 25, 2019 at 8:40
GrecdevGrecdev
9599 silver badges14 bronze badges
1 Answer
Reset to default 12The 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).