简体   繁体   English

最好导出包含函数的对象,或者只导出ES6中的多个函数(是否存在约定?)

[英]Better to export an object containing function, or just export multiple functions in ES6 (Is there a convention?)

This is a question of convention. 这是一个惯例问题。 I'm new to ES6 but I'm trying to make use of the module system. 我是ES6的新手,但我正在尝试使用模块系统。 Is preferred/more common to export multiple functions from a single file, or export a single object containing these functions. 从单个文件导出多个函数或导出包含这些函数的单个对象是首选/更常见的。

Example: 例:

utils.js utils.js

export function add(num1, num2) {
  return num1 + num2;
}

export function minus(num1, num2) {
  return num1 - num2;
}

and use it like this: 并像这样使用它:

import {add, minus} from 'utils.js';

vs VS

utils.js utils.js

const utils = {
  add: (num1, num2) => {
    return num1 + num2;
  },

  minus: (num1, num2) => {
    return num1 - num2;
  }
}

export default utils;

In a utils file that contains a 50-100 functions, it seems the second way would be the clear winner. 在包含50-100个函数的utils文件中,第二种方式似乎是明显的赢家。 But there's just something that feels wrong about it to me, and I don't know why. 但是对我来说只有一些感觉不对的东西,我不知道为什么。

Going forward, it's probably better to export multiple functions, as tree shaking allows module bundlers to eliminate dead code based on whether it's been imported or not. 展望未来,导出多个功能可能更好,因为树摇动允许模块捆绑器根据是否已导入来消除死代码。

If you export one large object, it's not always possible to use static analysis to tell which functions need to be kept and which can be safely discarded. 如果导出一个大对象,则无法始终使用静态分析来确定需要保留哪些函数以及哪些函数可以安全地丢弃。

If you export multiple named functions, then the module bundler can analyze the AST then safely whitelist the functions which you are actually importing. 如果导出多个命名函数,则模块捆绑器可以分析AST,然后将您实际导入的函数安全地列入白名单。

If you have as you say 50-100 functions that you want to expose through your utils file then I'd say go with the named exports 如果您有通过utils文件公开的50-100个函数,那么我会说使用命名的导出

export function add() {}

Since otherwise every time an import of utils would take place you'd import the all of the functions. 因为否则每次导入utils时都会导入所有函数。 This might be what you want sometimes but most likely there'll only be a handful of useable functions for any given module. 这可能是你有时想要的,但最有可能的是,对于任何给定的模块,只有少数可用的函数。 If you want all of the functions a simple import * as utils from './utils'; 如果你想要所有函数一个简单的import * as utils from './utils'; would suffice. 就够了

However there is nothing that doesn't say you can use both patterns if you want to safe guard against it. 但是,如果您想要安全防范它,那么没有什么不能说您可以使用这两种模式。

export function add() {};

const utils = {
   add: add
};
export default utils; 

Above is both valid and quite common as well. 以上是有效的,也很常见。

Just remember as of Babel 6.x when developing a library using the export default will actually produce an object (the correct way) containing a default property on which your exported object will be attached to. 只记得使用export default开发库时Babel 6.x实际上会生成一个对象(正确的方法),其中包含一个default属性,导出的对象将附加到该属性上。

import utils from './utils';

console.log(utils);
// { default: yourUtilsObject }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM