【问题标题】:NodeJS - Require and ModulesNodeJS - 需求和模块
【发布时间】:2016-03-03 13:50:38
【问题描述】:

NodeJS 中的requiremodule.exports 是否可以用于获取位于一个目录而不是单个JavaScript 文件中的所有JavaScript 文件中的所有函数?
如果是这样怎么办?
谁能用一个例子解释一下吗?

【问题讨论】:

标签: javascript node.js prototypal-inheritance


【解决方案1】:

如果 require 给出了目录路径,它将在该目录中查找 index.js 文件。因此,将您的模块特定的 js 文件放在一个目录中,创建一个 index.js 文件并最终在您的工作 js 文件中要求该目录。希望下面的例子有帮助....

例子:

文件:modules/moduleA.js

function A (msg) {
    this.message = msg;
}
module.exports = A;

文件:modules/moduleB.js

function B (num) {
    this.number = num;
}
module.exports = B;

文件:modules/index.js

module.exports.A = require("./moduleA.js");
module.exports.B = require("./moduleB.js");

文件:test.js

var modules = require("./modules");
var myMsg = new modules.A("hello");
var myNum = new modules.B("000");
console.log(myMsg.message);
console.log(myNum.number);

【讨论】:

    【解决方案2】:

    通过使用 require 您需要该文件中的模块,并且您可以使用该原型的所有功能(单个文件)而不是完整目录。 例如

        function admin(admin_id)
    {
        //console.log(parent_id);
        this.admin_id = admin_id;
    }
    //default constructor
    function admin()
    {
        admin_id = null;
        self =this;
    }
    //destructor
    ~function admin(){
        this.admin_id = null;
        console.log('admin obj destroyed!');
        }
    //exporting this class to access anywhere through data encapstulation
    module.exports = admin;
    //class methods
    
    admin.prototype =  {
    
            help:function(params){
    
    console.log('hi');
    }
    
        }, 
    

    你可以要求这个模块并且可以使用函数帮助 通过这种方法,您可以要求单个文件中的所有文件(模块)

    【讨论】:

      【解决方案3】:

      Wiki:“Node.js 是用于开发服务器端 Web 应用程序的开源、跨平台运行时环境。

      虽然 Node.js 不是 JavaScript 框架,但它的许多基础模块都是用 JavaScript 编写的,开发人员可以用 JavaScript 编写新模块。

      运行时环境使用 Google 的 V8 JavaScript 引擎解释 JavaScript。”

      Nodejs 示例:

      你有 Afile.js

      var Afile = function()
      {
      
      };
      
      Afile.prototype.functionA = function()
      {
          return 'this is Afile';
      }
      
      module.exports = Afile;
      

      和 Bfile.js

      var Bfile = function()
      {
      
      };
      
      Bfile.prototype.functionB = function()
      {
          return 'this is Bfile';
      }
      
      module.exports = Bfile;
      

      Test.js 文件需要 Afile.js 和 Bfile.js

      var Afile                         = require(__dirname + '/Afile.js');
      var Bfile                         = require(__dirname + '/Bfile.js');
      
      var Test = function()
      {
      
      };
      
      Test.prototype.start = function()
      {
          var Afile = new Afile();
          var Bfile = new Bfile();
      
          Afile.functionA();
          Bfile.functionB();
      }
      
      var test = Test;
      test.start();
      

      【讨论】:

        猜你喜欢
        • 2017-10-13
        • 2012-09-03
        • 2012-06-19
        • 1970-01-01
        • 2014-06-06
        • 2019-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多