【问题标题】:Using function inside function inside an object在对象内部的函数内部使用函数
【发布时间】:2019-10-16 07:49:17
【问题描述】:

我想调用函数 A 到函数 B。

我已经尝试从函数 B 调用函数 A

module.exports = function(req, res) {
  return {
    function_a: function() {
      // do something here
    },
    function_b: function() {
      // call function_a
      const A = function_a(); // I want to be called something like this
      // do something here
    }
  }
}

我希望函数 A 在函数 B 中被调用

【问题讨论】:

  • 请添加您喜欢如何调用函数。
  • 它非常简单,为了让你能够调用任何函数,它必须是可见的(在调用者的范围内可用)。
  • 您现有的代码有语法错误(您似乎混淆了对象文字和函数体语法)。您需要先解决这个问题,然后再担心如何从另一个功能访问一个功能(因为如何您解决该问题会严重影响您如何解决您所询问的问题)
  • 在这里你要告诉你的导出将是函数module.exports = function(req, res) { 然后当你这样做时function_a: function() { // do something here },
  • 已经编辑了函数。你能帮忙吗?

标签: javascript node.js function


【解决方案1】:

假设function_b是通过以下方式调用的:

your_module().function_b();

然后你就可以通过function_b里面的this来访问对象了。

this.function_a();

如果方法与对象分离,那么您将失去该连接,因此可能值得重写模块,这样您就有一个不依赖于this 值的引用。例如:

module.exports = function(req, res) {

  const my_return_value = {
    function_a: function() {
      // do something here
    },
    function_b: function() {
      const A = my_return_value.function_a();
    }
  };

  return my_return_value;
}

【讨论】:

  • 能否请您提供一个 If the method gets detached from the object, then you'll lose that connection 的示例
  • 哇,这工作简直完美。我将尝试从中获取逻辑。谢谢。
  • jQuery.ajax("some url").then( your_module().function_b ) 会将方法与对象分离(例如)。
【解决方案2】:

您当前拥有的代码不是有效的 JavaScript。您正在尝试将函数作为函数内的对象处理 (??)。

你可以这样做:首先在module.exports之外创建函数

function funcA(){
  console.log('Inside function A!');
}

function funcB(){
  funcA();
  console.log('inside function B!');
}

这将像任何两个函数一样。调用funcA() 将记录'Inside function A!',调用funcB() 将记录'Inside function A!' 'inside function B!'

然后您可以将它们导出以在其他地方使用:

exports = {
  funcA: funcA,
  funcB: funcB
}

这里我们使用exports 而不是module.exports,这是等价的。上面可以用 ES6 简化:{funcA, funcB},因为键和值具有相同的名称。

【讨论】:

    【解决方案3】:

    试试这个。您还可以访问此链接以查看处理问题的不同方式。 https://gist.github.com/kimmobrunfeldt/10848413

    function a() {
    
    }
    
    function b() {
        a()
    }
    
    module.exports = {
        a: a,
        b: b
    }
    

    【讨论】:

      【解决方案4】:

      要在对象中引用另一个函数,您可以使用this 关键字。

      这个 sn-p 可能对你有帮助。

      function name (firstName, lastName) {
        return {
          function_a: function() {
      	return firstName
          },
          function_b: function() {
            const A = this.function_a(); 
      	return A + lastName 
          }
        }
      }
      
      const initName = name('rohit', 'bhatia') 
      
      console.log(initName.function_b())

      另外,昆汀的回答可能更有帮助/有用

      【讨论】:

        【解决方案5】:

        您的代码在 javascript 中无效
        应该是

        module.exports = function(req, res) {
          const function_a = function() {
            // do something here
          }
          const function_b = function() {
            // call function_a
            function_a()
            // do something here
          }
        }
        

        或者您可能的意思是导出对象而不是函数

        module.exports = {
          function_a:function() {
            // do something here
          },
          function_b:function() {
            // call function_a
            this.function_a()
            // do something here
          }
        }
        

        【讨论】:

          【解决方案6】:

          这看起来很混乱,但我假设你想要这样的东西:

          
          module.exports = function(req, res) {
             functionA() {};
          
             functionB(){
               functionA();
             }
          }
          

          如果这不是您要寻找的答案,请告诉我们您想从模块中导出什么。

          【讨论】:

          • 我已经明白了逻辑,但我不知道该怎么做。并且您上面的代码无法使用。
          猜你喜欢
          • 2017-12-20
          • 2012-05-09
          • 1970-01-01
          • 2010-10-27
          • 1970-01-01
          • 2022-09-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多