如何正确地要求节点模块,导出其中的所有功能

时间:2021-10-16 23:30:34

I have a file stats.js.

我有一个文件stats.js。

It's contents are

它的内容是

(function () {
    func1 = function () {

    }
    func2 = function () {

    }

    module.exports = this;

}).call(this);

Alegedly, when I do

当我这么做的时候

var stats = require("./stats");

I should be able to get func1 and func2 with stats.func1, stats.func2, right?

我应该能够使用stats.func1,stats.func2来获取func1和func2,对吗?

Well, I can't. The stats object is just empty. A few traces in the stats.js revealed that "this" is also an empty object.

好吧,我不能。 stats对象只是空的。 stats.js中的一些痕迹显示“this”也是一个空对象。

What gives?

2 个解决方案

#1


0  

No it shouldnt? That format is nothing like what Node.js needs in order to do its job.

不,不应该吗?这种格式与Node.js完成其工作所需的格式完全不同。

"What gives" is that you didn't read up on how node works. Node.js isn't just "JavaScript", it's a proramming model with a much richer API and specific behaviours. Requires use the "module.exports" object, so it would be a good idea to actually read up on how to use node.

“给出了什么”是因为你没有读到节点的工作原理。 Node.js不只是“JavaScript”,它是一个具有更丰富的API和特定行为的编程模型。需要使用“module.exports”对象,因此实际阅读如何使用节点是一个好主意。

mything.js:

var func3 = function() { ... },
    prop = "something";
...
module.exports = {
  func1: function() { ... },
  func2: function() { ... },
  func3: funct3,
  prop: prop,
  ...
};

which is identical to:

这与:

var func3 = function() { ... },
    prop = "something",
    ...
    MyLib = {
      func1: function() { ... },
      func2: function() { ... },
      func3: funct3,
      prop: prop,
      ...
    };
...
module.exports = MyLib;

app.js:

var mything = require("mything);

#2


1  

First and foremost see this link.

首先要看到这个链接。

Now lets see your code -

现在让我们看看你的代码 -

    var stats = require("./stats");
    //My steps - 

    //First Log
    console.log(stats.func1); // returns undefined
    //Second Log
    console.log(global.func1, global === GLOBAL); // returns [Function], true

Take aways from this code -
1. In the browser the global object is window object.
2. In node.js it is the global object.
3. Defining something using var in a module will only create a variable with a module scope.
4. Defining something without the var keyword will create a variable in the global scope.

取消此代码 - 1.在浏览器中,全局对象是窗口对象。 2.在node.js中,它是全局对象。 3.使用模块中的var定义某些内容只会创建一个带有模块范围的变量。 4.在没有var关键字的情况下定义内容将在全局范围内创建变量。

So func1 and func2 were defined in the global scope. Passing this to module.exports will pass the current module object only.

所以func1和func2是在全局范围内定义的。将此传递给module.exports将仅传递当前模块对象。

hope it helps, happy coding!

希望它有所帮助,快乐编码!

#1


0  

No it shouldnt? That format is nothing like what Node.js needs in order to do its job.

不,不应该吗?这种格式与Node.js完成其工作所需的格式完全不同。

"What gives" is that you didn't read up on how node works. Node.js isn't just "JavaScript", it's a proramming model with a much richer API and specific behaviours. Requires use the "module.exports" object, so it would be a good idea to actually read up on how to use node.

“给出了什么”是因为你没有读到节点的工作原理。 Node.js不只是“JavaScript”,它是一个具有更丰富的API和特定行为的编程模型。需要使用“module.exports”对象,因此实际阅读如何使用节点是一个好主意。

mything.js:

var func3 = function() { ... },
    prop = "something";
...
module.exports = {
  func1: function() { ... },
  func2: function() { ... },
  func3: funct3,
  prop: prop,
  ...
};

which is identical to:

这与:

var func3 = function() { ... },
    prop = "something",
    ...
    MyLib = {
      func1: function() { ... },
      func2: function() { ... },
      func3: funct3,
      prop: prop,
      ...
    };
...
module.exports = MyLib;

app.js:

var mything = require("mything);

#2


1  

First and foremost see this link.

首先要看到这个链接。

Now lets see your code -

现在让我们看看你的代码 -

    var stats = require("./stats");
    //My steps - 

    //First Log
    console.log(stats.func1); // returns undefined
    //Second Log
    console.log(global.func1, global === GLOBAL); // returns [Function], true

Take aways from this code -
1. In the browser the global object is window object.
2. In node.js it is the global object.
3. Defining something using var in a module will only create a variable with a module scope.
4. Defining something without the var keyword will create a variable in the global scope.

取消此代码 - 1.在浏览器中,全局对象是窗口对象。 2.在node.js中,它是全局对象。 3.使用模块中的var定义某些内容只会创建一个带有模块范围的变量。 4.在没有var关键字的情况下定义内容将在全局范围内创建变量。

So func1 and func2 were defined in the global scope. Passing this to module.exports will pass the current module object only.

所以func1和func2是在全局范围内定义的。将此传递给module.exports将仅传递当前模块对象。

hope it helps, happy coding!

希望它有所帮助,快乐编码!