node.js 从module.exports 中的另一个函数调用module.exports 中的“本地”函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10462223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:39:44  来源:igfitidea点击:

Call a "local" function within module.exports from another function in module.exports?

node.jsexpress

提问by k00k

How do you call a function from within another function in a module.exportsdeclaration?

如何在module.exports声明中从另一个函数中调用一个函数?

应用程序.js
var bla = require('./bla.js');
console.log(bla.bar());
bla.js
module.exports = {

  foo: function (req, res, next) {
    return ('foo');
  },

  bar: function(req, res, next) {
    this.foo();
  }

}

I'm trying to access the function foofrom within the function bar, and I'm getting:

我试图foo从函数内部访问该函数bar,我得到:

TypeError: Object # has no method 'foo'

类型错误:对象 # 没有方法 'foo'

If I change this.foo()to just foo()I get:

如果我this.foo()改为只是foo()我得到:

ReferenceError: foo is not defined

参考错误:未定义 foo

回答by k00k

Change this.foo()to module.exports.foo()

更改this.foo()module.exports.foo()

回答by Brett

You could declare your functions outside of the module.exportsblock.

您可以在module.exports块之外声明您的函数。

var foo = function (req, res, next) {
  return ('foo');
}

var bar = function (req, res, next) {
  return foo();
}

Then:

然后:

module.exports = {
  foo: foo,
  bar: bar
}

回答by Calvin Alvin

You can also do this to make it more concise and readable. This is what I've seen done in several of the well written open sourced modules:

您也可以这样做以使其更简洁和可读。这是我在几个编写良好的开源模块中看到的:

var self = module.exports = {

  foo: function (req, res, next) {
    return ('foo');
  },

  bar: function(req, res, next) {
    self.foo();
  }

}

回答by Ville

You can also save a reference to module's global scope outside the (module.)exports.somemodule definition:

您还可以在 (module.)exports.somemodule 定义之外保存对模块全局范围的引用:

var _this = this;

exports.somefunction = function() {
   console.log('hello');
}

exports.someotherfunction = function() {
   _this.somefunction();
}

回答by goozbox

Another option, and closer to the original style of the OP, is to put the object you want to export into a variable and reference that variable to make calls to other methods in the object. You can then export that variable and you're good to go.

另一种更接近 OP 原始样式的选择是将您要导出的对象放入一个变量中并引用该变量以调用对象中的其他方法。然后您可以导出该变量,一切顺利。

var self = {
  foo: function (req, res, next) {
    return ('foo');
  },
  bar: function (req, res, next) {
    return self.foo();
  }
};
module.exports = self;

回答by david_adler

const Service = {
  foo: (a, b) => a + b,
  bar: (a, b) => Service.foo(a, b) * b
}

module.exports = Service

回答by m.spyratos

Starting with Node.js version 13you can take advantage of ES6 Modules.

Node.js 版本 13开始,您可以利用ES6 模块

export function foo() {
    return 'foo';
}

export function bar() {
    return foo();
}


Following the Class approach:

遵循类方法:

class MyClass {

    foo() {
        return 'foo';
    }

    bar() {
        return this.foo();
    }
}

module.exports = new MyClass();

This will instantiate the class only once, due to Node's module caching:
https://nodejs.org/api/modules.html#modules_caching

由于 Node 的模块缓存,这只会实例化该类一次:https:
//nodejs.org/api/modules.html#modules_caching

回答by Akash Jain

To fix your issue, i have made few changes in bla.js and it is working,

为了解决您的问题,我在 bla.js 中做了一些更改并且它正在工作,

var foo= function (req, res, next) {
  console.log('inside foo');
  return ("foo");
}

var  bar= function(req, res, next) {
  this.foo();
}
module.exports = {bar,foo};

and no modification in app.js

并且在 app.js 中没有修改

var bla = require('./bla.js');
console.log(bla.bar());