简体   繁体   English

NodeJS - 从其他文件导入函数并使用它们之前就好了吗?

[英]NodeJS - Import functions from other file and use as if it had them before?

Let's say I have a file called server.js : 假设我有一个名为server.js的文件:

//server.js
    var net = require('net');
    var server = net.createServer(function (socket){
    socket.on('data',function(data){
        console.log("Received: "+data);
    });
});

And another file called functions.js : 另一个名为functions.js文件:

//functions.js
module.exports = {
  test: function () {
    // do something
  },
  other: function () {
    // do something
  }
};

I know I can do something like this: 我知道我可以这样做:

var functions = require('./functions');
functions.test();

But I really wanted to be able to use the function directly, like this: 但我真的希望能够直接使用该功能,如下所示:

require('./functions');
test();

Any suggestions? 有什么建议么?

While you could load them into the global variable, another possible, and perhaps more sane* way to do that would be to assign the method to the variable test . 虽然您可以将它们加载到global变量中,但另一种可能的,也许更理智的方法是将方法分配给变量test

var test = require('./functions').test;
var functions = require('./functions');
var test2 = functions.test;

* = I say sane, because touching the global scope can have some unexpected results, cuts down on general readability, (Hey, where did this test variable come from? I didn't see it defined anywhere in the code.), and worst case scenario, you might run into name clashes, or even overwrite something you need in the global scope. * =我说理智,因为触及全局范围会产生一些意想不到的结果,降低一般可读性,(嘿,这个test变量来自哪里?我没有看到它在代码中的任何地方定义。),最糟糕的是在这种情况下,您可能遇到名称冲突,甚至覆盖global范围内您需要的内容。 Not to say that loading some things into the global scope isn't a good idea, or even downright useful, but often times, it's best to keep things scoped locally for readability. 并不是说将一些东西加载到全局范围内并不是一个好主意,甚至是非常有用的,但通常情况下,为了便于阅读,最好保持本地范围内的内容。

An article on touching the global scope in the browser . 有关触摸浏览器中的全局范围的文章

Well, you could put the functions into the global variable so that they become accessible from anywhere in your code, but this is generally discouraged in favour of the preferred usage, as you already outlined in your code example. 好吧,您可以将函数放入global变量中,以便可以从代码中的任何位置访问它们,但通常不鼓励使用首选用法,如您在代码示例中所述。

If you insist on putting the variables in global scope, you can do the following: 如果您坚持将变量放在全局范围内,则可以执行以下操作:

// function.js
global.test = function test () {/* ... */}
// ...

// Somewhere else...
require('./functions')
test()

To at least partially improve on this, you could implement it so that the function.js file really only exports the functions as you currently do, and the individual functions get assigned to the global scope only by the requiring module. 为了至少部分地对此进行改进,您可以实现它,以便function.js文件实际上只导出您当前所执行的功能,并且各个函数仅由需求模块分配给global范围。 This way, you could at least properly test the functions. 这样,您至少可以正确地测试这些功能。

Update: You did not say whether or not you would like the function to be available like that throughout the whole application or only in the current scope (where you require them) - if you only need them to be in current scope, it is then preferred to steer clear of the global variable and instead assign the functions to variables: 更新:您没有说明您是否希望在整个应用程序中可以使用该功能,或者只在当前范围内(您require它们) - 如果您只需要它们在当前范围内,那么它就是首选避开global变量,而是将函数分配给变量:

var functions = require('./functions')
  , test = functions.test
  , other = functions.other

// Use as necessary
test()

Try this: 尝试这个:

require('./functions')();
test();

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

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