最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS - Import functions from other file and use as if it had them before? - Stack Overflow

programmeradmin4浏览0评论

Let's say I have a file called 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
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?

Let's say I have a file called 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
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?

Share Improve this question asked Feb 24, 2015 at 20:44 MagisterMundusMagisterMundus 3455 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

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.

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 e 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. 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 bee 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.

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. 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:

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

// Use as necessary
test()

Try this:

require('./functions')();
test();
发布评论

评论列表(0)

  1. 暂无评论