简体   繁体   English

node.js中模块模式的最佳实践

[英]best practice for module patterns in node.js

I have been converting some old javascript for use in a node.js module and it got me thinking about module pattern options. 我一直在转换一些旧的javascript以在node.js模块中使用,这让我开始思考模块模式选项。

I have seen a number of structures using exports. 我已经看到许多使用出口的结构。 module.exports and even prototype, and it has left me wondering which method is considered best practice and why? module.exports甚至是原型,这让我想知道哪种方法被认为是最佳实践,为什么?

Here is a cut down module example from my code written in two ways. 这是我用两种方式编写的代码的简化模块示例。

Option 1: 选项1:

var Helper = function() {};
Helper.latin_map = {"Á":"A","Ă":"A","Ắ":"A","Ặ":"A","Ằ":"A"};

Helper.prototype.urlise = function(orgString){
    var lower = orgString.toLowerCase();

    var latinised = lower.replace(/[^A-Za-z0-9\[\] ]/g, function(a) {
        return Helper.latin_map[a] || a;
    });

   return latinised.replace(/\s+/g, '-')
}

module.exports = new Helper();

Option 2: 选项2:

var latin_map = {"Á":"A","Ă":"A","Ắ":"A","Ặ":"A","Ằ":"A"};

module.exports = {
    urlise : function(orgString){
       var lower = orgString.toLowerCase();
       var latinised = lower.replace(/[^A-Za-z0-9\[\] ]/g, function(a) {
           return latin_map[a] || a;
       });

       return latinised.replace(/\s+/g, '-')
   }
}

This is quite a simple example, but I will be extending this to provide several accessible functions within the same module, so before I allow things to get too complicated. 这是一个非常简单的示例,但是我将对其进行扩展以在同一模块内提供几个可访问的功能,因此在我让事情变得太复杂之前。 I thought I would seek some advice on which approach is considered best practice. 我想我会就哪种方法被认为是最佳实践征求一些意见。

I have seen a number of structures using exports , module.exports and even prototype, and it has left me wondering which method is considered best practice and why? 我已经看到了使用一些结构的exportsmodule.exports甚至原型,它给我留下了不知道哪种方法被认为是最好的做法,为什么?

Things which had a .prototype were constructor functions, and the module exported them as a class to be instantiated. 具有.prototype是构造函数,模块将它们导出为要实例化的

Exporting normal objects (as literals usually) or even extending the by-default-empty exports object is used when you want to export a "singleton" object with static properties, a "namespace" so to say. 当您要导出具有静态属性的“单例”对象(可以说是“命名空间”)时,将使用导出普通对象(通常是按原义)或什至扩展默认为空的exports对象。

  module.exports = new Helper() 

This is almost always wrong . 这几乎总是错误的 The Helper is totally unnecessary, and typically even objectionable. Helper是完全不必要的,并且通常甚至令人反感。

You should go with your Option 2. If you really did export only a single function you might consider module.exports = function urlise(orgString){…} , but to export several functions accessible on the module your Option 2 is exactly the pattern to use. 您应该使用module.exports = function urlise(orgString){…} 。如果确实只导出了一个函数,则可以考虑module.exports = function urlise(orgString){…} ,但是要导出模块上可访问的多个函数,则Option 2正是该模式采用。

Option 2 is better. 选项2更好。 You don't need constructor functions and OO instances unless you plan to do real OO programming where you create many instances and each has different data associated with it. 您不需要构造函数和OO实例,除非您打算进行真正的OO编程,在该编程中您将创建许多实例,并且每个实例都具有与之关联的不同数据。 This code works better as a simple function. 此代码作为一个简单的函数效果更好。

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

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