ES6 modules: Export single class of static methods OR multiple individual methods

前端 未结 2 1679
悲哀的现实
悲哀的现实 2020-11-22 14:48

I\'m using ECMAScript6 modules. What is the correct way to export/import multiple methods from a module from the options below?

Single class of static methods:

2条回答
  •  醉话见心
    2020-11-22 15:18

    TLDR; Use multiple exported methods and explicit import.

    @Bergi is right about not needing a class with static fields, only an object in your first case. However, this option is discouraged by Axel Rauschmayer:

    Note that default-exporting objects is usually an anti-pattern (if you want to export the properties). You lose some ES6 module benefits (tree-shaking and faster access to imports).

    Devs at Airbnb recommend named exports and explicit wildcrad import, see this thread: https://github.com/airbnb/javascript/issues/710#issuecomment-297840604

提交回复
热议问题