Idiomatic Revealing Module Pattern for ES6

后端 未结 1 1940
再見小時候
再見小時候 2021-01-31 10:46

In the past I\'ve used the revealing module pattern.

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

  return { 
    foo: foo, 
    bar: bar
  }         


        
相关标签:
1条回答
  • 2021-01-31 11:38

    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.

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

    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';
    

    as well as namespaces

    import * as baz from './export-file';
    
    0 讨论(0)
提交回复
热议问题