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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12274381/
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-10-26 15:47:36  来源:igfitidea点击:

Node JS - Calling a method from another method in same file

javascriptnode.js

提问by Veera

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()'.

我需要bar()从方法内部调用该foo()方法。我想 this.bar()还有bar(),但都失败说TypeError: Object #<Object> has no method 'bar()'

How can I call one method from the other?

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

采纳答案by timidboy

You can do it this way:

你可以这样做:

module.exports = {

  foo: function(req, res){

    bar();

  },
  bar: bar
}

function bar() {
  ...
}

No closure is needed.

不需要关闭。

回答by Nicolas Bonnici

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

回答by ChaosPandion

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

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

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

回答by Lucas Green

Try this:

试试这个:

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

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

        bar: realBar
    };
}());

回答by Chanaka Fernando

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;

回答by Nassim

in Node js + Express, you can use this syntax in the same controller

在 Node js + Express 中,你可以在同一个控制器中使用这个语法

//myController.js
exports.funA = () =>{

    console.log("Hello from funA!")

    //cal funB
    funB()

}

let funB = () =>{

    console.log("Hello from funB!")
}

make sure to use the letkeyword before the function and call it with the ()parentheses inside the main function

确保在函数前使用let关键字,并在主函数内使用()括号调用它

OUTPUT

输出

App listening at http://localhost:3000
Hello from fun A!
Hello from fun B!

回答by user323774

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

bar 是否旨在成为 foo 的内部(私有)?

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