简体   繁体   English

Node JS 在 module.exports 中调用“本地”函数

[英]Node JS call a "local" function within module.exports

How do you call a function from within another function in a module.exports declaration?如何从 module.exports 声明中的另一个函数中调用函数?

I have MVC structure node js project and a controller called TestController.js.我有 MVC 结构节点 js 项目和一个名为 TestController.js 的控制器。 I want to access method within controller, but using this keyword gives below error:我想访问控制器内的方法,但使用this关键字会出现以下错误:

cannot call method getName of undefined无法调用未定义的方法 getName

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

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

How do I access methods within controller?如何访问控制器中的方法?

I found the solution :-)我找到了解决方案:-)

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

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

You can access the getName function trough module.exports .您可以通过module.exports访问getName函数。 Like so:像这样:

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

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

Maybe you could do it like this.也许你可以这样做。 It reduce nesting.它减少了嵌套。 And all your export is done at the end of your file.所有导出都在文件末尾完成。

"use strict";

var _getName = function() {
    return 'john';
};

var _myName = function() {
    return _getName();
};

module.exports = {
    getName : _getName,
    myName : _myName
};

If you want to use the function locally AND in other files...如果您想在本地和其他文件中使用该功能...

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

I know the answer is already accepted, but I feel the need to add my two cents on the issue.我知道答案已经被接受,但我觉得有必要在这个问题上加两分钱。

Node modules have a "Singletonic" nature, when inside the module, you are the module.节点模块具有“单一”性质,当在模块内部时,您就是模块。 In my opinion, at least design pattern wise, inner module methods can be accessed more cleanly, without the need for this or a copy of self for that matter.在我看来,至少在设计模式方面,内部模块方法可以更干净地访问,而无需thisself的副本。

Using this , could be dangerous, if one happens to send the separate methods around and forgets to use .bind .使用this可能是危险的,如果有人碰巧发送单独的方法而忘记使用.bind

Using a copy of self , is redundant, we already are inside a Singleton behaving module, why keep a reference to yourself when you can avoid that?使用self的副本是多余的,我们已经在一个单例行为模块中,为什么在可以避免的情况下保留对自己的引用?

Consider these instead:考虑这些:

Option 1选项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)
}

Option 2选项 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
}

This works the same for es6 modules:这对 es6 模块同样有效:

Option 1 (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)
}

Option 2 (ES6)选项 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
}

Again, this is just an opinion, but it looks cleaner, and is more consistent across different implementations, and not a single this / self is used.同样,这只是一个意见,但它看起来更清晰,并且在不同的实现中更加一致,并且没有使用单个this / self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM