简体   繁体   English

CommonJS函数声明范围的良好实践

[英]CommonJS function declaration scope good practice

Is there a good practice for where you declare a function inside a CommonJS module in a case like this: 在这种情况下,在CommonJS模块中声明函数的位置是否有良好的做法:

// function foo() { ... }

module.exports = function () {
   // function foo () { ... }

   return function () {
        // function foo () { ... }

        return foo();
   };
};

Where should function foo {} go ? function foo {}应该去哪里? Of course I'm assuming you really have the choice between the 3 possibilities in your module code and that all of them would work properly as intended. 当然,我假设您可以在模块代码中的3种可能性之间做出选择,并且所有这些可能都按预期正常工作。 I use a function as an example as I think it makes my question clearer, but it also goes for any code logic that can be encapsulated in those scopes. 我使用函数作为示例,因为我认为它使我的问题更清楚,但它也适用于可以封装在这些范围内的任何代码逻辑。

The point of module.exports is to make something available for import in other modules and that's really all it does. module.exports的目的是让其他模块可以导入一些东西,这就是它的全部功能。

This means that the same conventions that you would have used without module.exports apply here. 这意味着您在没有module.exports的情况下使用的相同约定适用于此处。

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

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