不必要地需要整个module.exports的不利之处是什么?

时间:2018-06-22 19:42:06

标签: javascript node.js require

我只需要一个模块(总共6个),但是这样做会使我需要的模块的可读性降低。例如,不要这样做:

const { requestKrakenOrderbook } = require('./krakenUtils');
const orderbook = requestKrakenOrderbook();

我想这样做:

const krakenUtils = require('./krakenUtils')
const orderbook = krakenUtils.requestOrderbook()

要求像这样的整个模块有什么弊端?

那呢?

const getKrakenOrderbook = require('./krakenUtils').getOrderbook

1 个答案:

答案 0 :(得分:1)

没有缺点。 CommonJS模块是在"celebrities"上进行评估的,整个模块的导出通常都已缓存,因此性能或内存占用没有明显差异。

取决于导出的模块是什么,将整个模块存储到变量并将其用作命名空间可能有用或不有用:

{
  "categories": [
    {
      "name": "people_",
      "score": 0.94140625,
      "detail": {
        "celebrities": [],
        "landmarks": null
      }
    }
  ],
  "adult": null,
  "tags": [
    {
      "name": "person",
      "confidence": 0.99912232160568237
    },
    {
      "name": "outdoor",
      "confidence": 0.936089813709259
    }
  ],
  "description": {
    "tags": [
      "person",
      "outdoor",
      "man",
      "woman",
      "standing",
      "front",
      "suit",
      "holding",
      "car",
      "wearing",
      "people",
      "couple",
      "posing",
      "young",
      "bus",
      "black",
      "smiling",
      "glasses",
      "umbrella",
      "group",
      "table",
      "city",
      "red",
      "water",
      "street",
      "phone",
      "boat",
      "train"
    ],
    "captions": [
      {
        "text": "a couple of people posing for the camera",
        "confidence": 0.86922603629725859
      }
    ]
  },
  "requestId": "7d57493c-f8b0-468f-8135-17da555d2463",
  "metadata": {
    "width": 1000,
    "height": 1000,
    "format": "Jpeg"
  },
  "faces": [
    {
      "age": 38,
      "gender": "Female",
      "faceRectangle": {
        "left": 400,
        "top": 270,
        "width": 234,
        "height": 234
      }
    }
  ],
  "color": {
    "dominantColorForeground": "Black",
    "dominantColorBackground": "Black",
    "dominantColors": [
      "Black",
      "Grey"
    ],
    "accentColor": "364E5D",
    "isBWImg": false
  },
  "imageType": {
    "clipArtType": 0,
    "lineDrawingType": 0
  }
}

vs。

require