Node JS - 从同一文件中的另一个方法调用方法

时间:2021-01-13 16:03:02

I have this nodeJS code.

我有这个nodeJS代码。

module.exports = {

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

  bar: function(){
    ...
    ...
  }
}

I need to call the bar() method from inside the foo() method. I tried this.bar() as well as bar(), but both fail saying TypeError: Object #<Object> has no method 'bar()'.

我需要从foo()方法中调用bar()方法。我尝试了this.bar()以及bar(),但都失败了说TypeError:Object#没有方法'bar()'。

How can I call one method from the other?

如何从另一个方法调用一个方法?

6 个解决方案

#1


6  

Do it this way:

这样做:

module.exports = {

  foo: function(req, res){

    bar();

  },
  bar: bar
}

function bar() {
  ...
}

No closure is needed.

不需要关闭。

#2


2  

I think what you can do is bind the context before passing the callback.

我认为你可以做的是在传递回调之前绑定上下文。

something.registerCallback(module.exports.foo.bind(module.exports));

#3


1  

Try this:

尝试这个:

module.exports = (function () {
    function realBar() {
        console.log('works');
    }
    return {

        foo: function(){
            realBar();
        },

        bar: realBar
    };
}());

#4


1  

The accepted response is wrong, you need to call the bar method from the current scope using the "this" keyword:

接受的响应是错误的,您需要使用“this”关键字从当前作用域调用bar方法:

    module.exports = {
      foo: function(req, res){

        this.bar();

      },
      bar: function() { console.log('bar'); }
    }

#5


0  

Is bar intended to be internal (private) to foo?

酒吧是内部(私人)foo?

module.exports = {
    foo: function(req, res){
        ...
        function bar() {
            ...
            ...
        }
        bar();     
        ...
    }
}

#6


0  

Try the following code. You can refer each function from anywhere (needs to import .js file)

请尝试以下代码。你可以从任何地方引用每个函数(需要导入.js文件)

function foo(req,res){
    console.log("you are now in foo");
    bar();
}
exports.foo = foo;

function bar(){
    console.log("you are now in bar");
}
exports.bar = bar;

#1


6  

Do it this way:

这样做:

module.exports = {

  foo: function(req, res){

    bar();

  },
  bar: bar
}

function bar() {
  ...
}

No closure is needed.

不需要关闭。

#2


2  

I think what you can do is bind the context before passing the callback.

我认为你可以做的是在传递回调之前绑定上下文。

something.registerCallback(module.exports.foo.bind(module.exports));

#3


1  

Try this:

尝试这个:

module.exports = (function () {
    function realBar() {
        console.log('works');
    }
    return {

        foo: function(){
            realBar();
        },

        bar: realBar
    };
}());

#4


1  

The accepted response is wrong, you need to call the bar method from the current scope using the "this" keyword:

接受的响应是错误的,您需要使用“this”关键字从当前作用域调用bar方法:

    module.exports = {
      foo: function(req, res){

        this.bar();

      },
      bar: function() { console.log('bar'); }
    }

#5


0  

Is bar intended to be internal (private) to foo?

酒吧是内部(私人)foo?

module.exports = {
    foo: function(req, res){
        ...
        function bar() {
            ...
            ...
        }
        bar();     
        ...
    }
}

#6


0  

Try the following code. You can refer each function from anywhere (needs to import .js file)

请尝试以下代码。你可以从任何地方引用每个函数(需要导入.js文件)

function foo(req,res){
    console.log("you are now in foo");
    bar();
}
exports.foo = foo;

function bar(){
    console.log("you are now in bar");
}
exports.bar = bar;