呼叫“本地” module.exports中的module.exports中的另一个函数的功能? [英] Call a "local" function within module.exports from another function in module.exports?

查看:148
本文介绍了呼叫“本地” module.exports中的module.exports中的另一个函数的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这里有一些简化的代码。

Here's some simplified code.

在我的app.js中,我执行以下操作:

In my app.js, I do the following:

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();
  }

}

我正在尝试访问功能 foo 从函数 bar 中,我得到:

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

TypeError:Object#没有方法'foo'

如果我更改 this.foo() foo()我得到:

ReferenceError:foo未定义

推荐答案

我想我得到了我刚刚将 this.foo()更改为 module.exports.foo(),似乎工作正常。

I think I got it. I just changed this.foo() to module.exports.foo() and it seems to be working.

如果有人拥有更好或更适当的方法,请随时更正我。

If someone has a better or more proper method, please feel free to correct me.

这篇关于呼叫“本地” module.exports中的module.exports中的另一个函数的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆