【问题标题】:Node JS call a "local" function within module.exportsNode JS 在 module.exports 中调用“本地”函数
【发布时间】:2015-10-12 10:32:34
【问题描述】:

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

我有 MVC 结构节点 js 项目和一个名为 TestController.js 的控制器。我想访问控制器中的方法,但使用 this 关键字会出现以下错误:

无法调用未定义的getName方法

"use strict"
module.exports = {
    myName : function(req, res, next) {
        // accessing method within controller
        this.getName(data);
    },

    getName : function(data) {
        // code
    }
}

如何访问控制器中的方法?

【问题讨论】:

  • this 的值取决于执行上下文,而不是函数的定义方式。

标签: javascript node.js model-view-controller express


【解决方案1】:

我找到了解决方案:-)

"use strict"
var self = module.exports = {
    myName : function(req, res, next) {
        // accessing method within controller
        self.getName(data);
    },

    getName : function(data) {
        // code
    }
}

【讨论】:

  • this.getName(data) 仍然可以使用,但分配命名约定是个好主意
【解决方案2】:

您可以通过module.exports 访问getName 功能槽。像这样:

"use strict"
module.exports = {
    myName : function(req, res, next) {
        // accessing method within controller
        module.exports.getName(data);
    },

    getName : function(data) {
        // code
    }
}

【讨论】:

    【解决方案3】:

    也许你可以这样做。它减少了嵌套。您的所有导出都在文件末尾完成。

    "use strict";
    
    var _getName = function() {
        return 'john';
    };
    
    var _myName = function() {
        return _getName();
    };
    
    module.exports = {
        getName : _getName,
        myName : _myName
    };
    

    【讨论】:

    • 这种方法的另一个好处是函数可以相互引用(即_myName可以调用_getName
    • 绝对,我认为它也比其他解决方案更优雅。 self 没有用,你可以在模块中添加私有函数。
    【解决方案4】:

    如果您想在本地和其他文件中使用该功能...

    function myFunc(){
        return 'got it'
    }
    module.exports.myFunc = myFunc;
    

    【讨论】:

      【解决方案5】:

      我知道答案已经被接受,但我觉得有必要在这个问题上加两分钱。

      节点模块具有“单调”性质,在模块内部时,您就是模块。 在我看来,至少在设计模式方面,可以更干净地访问内部模块方法,而无需 thisself 的副本。

      使用 this 可能很危险,如果碰巧发送了单独的方法并忘记使用 .bind

      使用self 的副本是多余的,我们已经在一个单例行为模块中,既然可以避免这种情况,为什么还要保留对自己的引用?

      请考虑这些:

      选项 1

      // using "exports."
      
      exports.utilityMethod = (..args) => {
           // do stuff with args
      }
      
      exports.doSomething = (someParam) => {
          // this always refers to the module
          // no matter what context you are in
          exports.utility(someParam)
      }
      
      

      选项 2

      // using module.exports
      
      const utility = (..args) => {
         // do stuff with args
      }
      
      const doSomething = (someParam) => {
          // Inside the module, the utility method is available
          // to all members
          utility(someParam)
      }
      
      // either this
      module.exports = {
       utility,
       doSomething,
      }
      
      // or 
      module.exports = {
       customNameForUtility: utility,
       customNameForDoSomething: doSomething
      }
      

      这对 es6 模块同样适用:

      选项 1 (ES6)

      export const utilityMethod = (..args) => {
           // do stuff with args
      }
      
      export const doSomething = (someParam) => {
          // this always refers to the module
          // no matter what context you are in
          utility(someParam)
      }
      

      选项 2 (ES6)

      const utility = (..args) => {
         // do stuff with args
      }
      
      const doSomething = (someParam) => {
          // Inside the module, the utility method is available
          // to all members
          utility(someParam)
      }
      
      export default {
        doSomething,
        utility
      }
      
      // or 
      export {
       doSomething,
        utility
      }
      

      同样,这只是一个观点,但它看起来更简洁,并且在不同的实现中更加一致,并且没有使用单个 this/self

      【讨论】:

        猜你喜欢
        • 2012-05-14
        • 2018-11-21
        • 2020-10-22
        • 1970-01-01
        • 2019-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-03
        相关资源
        最近更新 更多