node.js:从其他文件访问导出的函数

时间:2021-12-21 16:02:21

I have the following code:

我有以下代码:

// jira.js
var exports     = module.exports = {};

module.exports = function () {
  var exported = {};
  ..
  exported.myMethod= function (bugId, done) {
     ..
  }
  ..
 return exported;
};

and i want to use the myMthod function from other file bugs.js in the same directory:

我想在同一目录中使用其他文件bugs.js中的myMthod函数:

var exports     = module.exports = {};
var jira = require('./jira');

module.exports = function () {
  var exported = {};
  ..
  exported.secondMethod= function (bugId, done) {
     jira.myMethod(...) {
     }
     ..
  }
  ..
 return exported;
};

When i try to access myMthod from bugs.js, i get ' undefined'. Upon doing console.log(jira) jyst above the call to jira.myMthod() , i see the entire js file logged out.

当我尝试从bugs.js访问myMthod时,我得到'undefined'。在调用jira.myMthod()之后执行console.log(jira)jyst时,我看到整个js文件已经注销。

How do i get this to work?

我如何让它工作?

Please advise,

thanks!

3 个解决方案

#1


1  

since your module.exports in jira is a function, you need to execute it in order to get the returned value (you can do it by requiring it like this: var jira = require('./jira')(); which will return the exported functions).

因为你在jira中的module.exports是一个函数,你需要执行它才能获得返回的值(你可以这样做:var jira = require('./ jira')();这将返回导出的函数)。

But in my mind, this is redundant. I prefer this syntax:

但在我看来,这是多余的。我更喜欢这种语法:

// jira.js

function myMethod (bugId, done) {
  ..
}
  ..
return {
  myMethod : myMethod
};

what this will make is when you require jira, it will run to code (in this case, define the function) and will return it

这将是什么,当你需要jira时,它将运行代码(在这种情况下,定义函数)并将返回它

#2


1  

When you require a module, the result of require(...) is whatever is assigned to module.exports in that module. In this case you're assigning a function that returns an object with the methods you want.

当您需要模块时,require(...)的结果是分配给该模块中的module.exports的结果。在这种情况下,您将分配一个函数,该函数返回具有所需方法的对象。

So either use it as:

所以要么用它:

var jira = require('./jira')();
jira.myMethod();

or change the jira.js to something like:

或者将jira.js改为:

var exports     = module.exports = {};
exports.myMethod = function (bugId, done) {
  ..
  }

#3


0  

This simplifies down to setting your exported object to a variable and then setting module.exports equal to that var. Then on import, just require the file and use the imported file as if it is a normal object.

这简化了将导出的对象设置为变量,然后将module.exports设置为等于该var。然后在导入时,只需要该文件并使用导入的文件,就像它是普通对象一样。

To export:

// jira.js
  var jira = {};
  ...
  jira.myMethod = function (bugId, done) {
     ...
  }
  ...
module.exports = jira;

To access:

//bugs.js
var jira = require('./jira');

  var bugs = {};
  ..
  bugs.secondMethod= function (bugId, done) {
     jira.myMethod(...);
     ..
  }
  ..

#1


1  

since your module.exports in jira is a function, you need to execute it in order to get the returned value (you can do it by requiring it like this: var jira = require('./jira')(); which will return the exported functions).

因为你在jira中的module.exports是一个函数,你需要执行它才能获得返回的值(你可以这样做:var jira = require('./ jira')();这将返回导出的函数)。

But in my mind, this is redundant. I prefer this syntax:

但在我看来,这是多余的。我更喜欢这种语法:

// jira.js

function myMethod (bugId, done) {
  ..
}
  ..
return {
  myMethod : myMethod
};

what this will make is when you require jira, it will run to code (in this case, define the function) and will return it

这将是什么,当你需要jira时,它将运行代码(在这种情况下,定义函数)并将返回它

#2


1  

When you require a module, the result of require(...) is whatever is assigned to module.exports in that module. In this case you're assigning a function that returns an object with the methods you want.

当您需要模块时,require(...)的结果是分配给该模块中的module.exports的结果。在这种情况下,您将分配一个函数,该函数返回具有所需方法的对象。

So either use it as:

所以要么用它:

var jira = require('./jira')();
jira.myMethod();

or change the jira.js to something like:

或者将jira.js改为:

var exports     = module.exports = {};
exports.myMethod = function (bugId, done) {
  ..
  }

#3


0  

This simplifies down to setting your exported object to a variable and then setting module.exports equal to that var. Then on import, just require the file and use the imported file as if it is a normal object.

这简化了将导出的对象设置为变量,然后将module.exports设置为等于该var。然后在导入时,只需要该文件并使用导入的文件,就像它是普通对象一样。

To export:

// jira.js
  var jira = {};
  ...
  jira.myMethod = function (bugId, done) {
     ...
  }
  ...
module.exports = jira;

To access:

//bugs.js
var jira = require('./jira');

  var bugs = {};
  ..
  bugs.secondMethod= function (bugId, done) {
     jira.myMethod(...);
     ..
  }
  ..