Nodejs模块的命名约定

时间:2014-05-20 16:33:07

标签: node.js naming-conventions

鉴于这种模式:

模块定义了一个公共API,但需要运行时信息。

// Contrived example for a module abstracting a db
module.exports = function (host, schema, ...) {

  // A "module-level", private, instance of an object
  // that will be used by the public API of the module
  var connection = getConnection(host, schema, ...);

  return {
      queryItems : function (arg) {
          // Something that uses both the "module" level instance
          // and runtime args.
          return connection.query(... arg ... ); 
      }
  }
};

另一个模块必须要求它,而不是" instanciate"模块,和 用它。

// Module "bar"
var db = require("db"),
    xxx = db("myhost", "myschema");

xxx.queryItems("Test"):

in" traditionnal" OO,第一个模块将公开一个类构造函数,并按惯例公开 有第一个大写字母,所以你要写

var Db = require("db"),
    db = new Db("....", "....");

并没有含糊不清; " var cat = new Cat()"非常接受; " var felix = new Cat()"只会在多个实例的情况下使用。

是否有"规范" /"惯用"在nodejs中,命名这样一个暴露函数的模块,以及这个函数返回的对象?

(对不起,如果太主观,我正在寻找"已建立的"惯例,而不是"最好的"一个。)

0 个答案:

没有答案
相关问题