最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Revealing module pattern combined with ES6 modules - Stack Overflow

programmeradmin6浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 12

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).

发布评论

评论列表(0)

  1. 暂无评论