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

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

问题描述

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

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

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

}

我试图从函数 bar 中访问函数 foo,我得到:

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

TypeError: Object # has no method 'foo'

TypeError: Object # has no method 'foo'

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

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

ReferenceError: foo 未定义

ReferenceError: foo is not defined

推荐答案

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

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

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