ES6的惯用揭示模块模式 [英] Idiomatic Revealing Module Pattern for ES6

查看:76
本文介绍了ES6的惯用揭示模块模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去我曾使用揭示模块模式

function myModule() {
  function foo() ...
  function bar() ...

  return { 
    foo: foo, 
    bar: bar
  };
}

使用ES6,使用对象速记改进了这一点。

With ES6, this was improved with object shorthand.

function myModule() {
  function foo() ...
  function bar() ...

  return { foo, bar };
}

现在有了内置模块语法,我很难找到首选与上述类似的模式。

Now with built-in module syntax, I'm struggling to find the preferred pattern that is most similar to the above.

选项#1名为exports

Option #1 named exports

// export file
function foo() ...
function bar() ...

export { foo, bar };

// import file
import { foo, bar } from './export-file';

foo();
bar();

选项#2默认导出/导入与解构

Option #2 default export/import with destructuring

// export file
function foo() ...
function bar() ...

export default { foo, bar };

// import file
import baz from './export-file';

const { foo, bar } = baz;

foo();
bar();

选项#3默认导出/导入,名称间距

Option #3 default export/import with name spacing

// export file
function foo() ...
function bar() ...

export default { foo, bar };

//import file
import baz from './export-file';

baz.foo();
baz.bar();

我喜欢选项#1和命名导出,因为它在解构导入语法中提供了简单性。

I like Option #1 with the named exports for the simplicity it offers in the "destructuring" import syntax.

import { foo, bar } from './export-file';

我还想继续在导出文件的底部明确定义模块的导出API导出对象。

I also want to continue to make the module's exported API explicitly defined at the bottom of the exporting file in the export object.

export { foo, bar };
// OR
export default { foo, bar };

我一直读到默认导出是首选,所以我一直试图找到一个包含默认导出的首选模式,但我不愿意采用,因为它看起来更加冗长而且几乎没有优势(除了需要名称间距,或者在某些情况下包含命名和默认导出)。

I read all the time that default exports are preferred, and so I've been trying to find a preferred pattern that includes default exports, but I'm reluctant to adopt as it just seems more verbose with few advantages (other than the need for name-spacing, or the inclusion of both named and default exports in some cases).

是否有一个具有ES6模块语法的揭示模块模式的惯用模式?

Is there an idiomatic pattern for the revealing module pattern with ES6 module syntax?

推荐答案


我一直读到默认导出是首选的

I read all the time that default exports are preferred

不,它们不是。它们更简单,语法更短,可能会更频繁地使用(因为有更多的小型单导出模块),但它们通常不是首选。

No, they are not. They are simpler and have shorter syntax, and might be used more often (since there are more small, single-export modules), but they are not generally preferred.

使用适合工作的正确工具。您已经知道了自己想要的优势。

Use the right tool for the job. You already know the advantages you want.


是否存在使用ES6模块语法显示模块模式的惯用模式?

Is there an idiomatic pattern for the revealing module pattern with ES6 module syntax?

是,选项#1。如果要导出多个内容,请始终使用命名导出。

Yes, Option #1. When you have multiple things to export, always use named exports.

您可以从挑剔的语法中获得显式别名和树抖动

You get both explicit aliasing and tree shaking from the picky syntax

import { foo, bar } from './export-file';

以及命名空间

import * as baz from './export-file';

这篇关于ES6的惯用揭示模块模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆