在Node.js中包含来自另一个文件的Javascript类定义

时间:2021-12-16 16:02:32

I'm writing a simple server for Node.js and I'm using my own class called User which looks like:

我正在为Node编写一个简单的服务器。js和我正在使用我自己的类User,看起来像:

function User(socket) {
    this.socket = socket;
    this.nickname = null;

    /* ... just the typical source code like functions, variables and bugs ... */

    this.write = function(object) {
        this.socket.write(JSON.stringify(object));
    }
};

and then later in the process I'm instantiating it a lot:

之后在这个过程中我多次实例化它

var server = net.createServer(function (socket) {
    /* other bugs */
    var user = new User(socket);
    /* more bugs and bad practise */
});

Can I move my User class definition to another javascript file and "include" it somehow?

我是否可以将用户类定义移动到另一个javascript文件并以某种方式“包含”它?

5 个解决方案

#1


93  

And even you can simply do this

甚至你也可以这么做

user.js

user.js

class User {
    //...
};

module.exports = User;

server.js

server.js

const User = require('./user.js');

// Instantiate User:
let user = new User();

P.S. And don't use globals. It creates potential conflicts in the future.

注意,不要使用全局变量。它在未来制造潜在的冲突。

#2


6  

Using ES6, you can have user.js:

使用ES6,您可以拥有user.js:

export default class User {
  constructor() {
    ...
  }
}

And then use it in server.js

然后在server.js中使用它

const User = require('./user.js').default;
const user = new User();

#3


5  

Modify your class definition to read like this:

修改类定义如下:

exports.User = function (socket) {
  ...
};

Then rename the file to user.js. Assuming it's in the root directory of your main script, you can include it like this:

然后将文件重命名为user.js。假设它位于主脚本的根目录中,您可以将其包括如下:

var user = require('./user');
var someUser = new user.User();

That's the quick and dirty version. Read about CommonJS Modules if you'd like to learn more.

这是快速而肮脏的版本。如果您想了解更多,请阅读CommonJS模块。

#4


5  

Another way in addition to the ones provided here for ES6

除了这里为ES6提供的其他方法

module.exports = class TEST{
    constructor(size) {
        this.map = new MAp();
        this.size = size;

    }

    get(key) {
        return this.map.get(key);
    }

    length() {
        return this.map.size;
    }    

}

and include the same as

并包含相同的as

var TEST= require('./TEST');
var test = new TEST(1);

#5


1  

If you append this to user.js:

如果你把这个附加到用户。js:

exports.User = User;

then in server.js you can do:

然后在服务器。js你能做什么:

var userFile = require('./user.js');
var User = userFile.User;

http://nodejs.org/docs/v0.4.10/api/globals.html#require

http://nodejs.org/docs/v0.4.10/api/globals.html要求

Another way is:

另一种方法是:

global.User = User;

then this would be enough in server.js:

这样在服务器上就足够了。

require('./user.js');

#1


93  

And even you can simply do this

甚至你也可以这么做

user.js

user.js

class User {
    //...
};

module.exports = User;

server.js

server.js

const User = require('./user.js');

// Instantiate User:
let user = new User();

P.S. And don't use globals. It creates potential conflicts in the future.

注意,不要使用全局变量。它在未来制造潜在的冲突。

#2


6  

Using ES6, you can have user.js:

使用ES6,您可以拥有user.js:

export default class User {
  constructor() {
    ...
  }
}

And then use it in server.js

然后在server.js中使用它

const User = require('./user.js').default;
const user = new User();

#3


5  

Modify your class definition to read like this:

修改类定义如下:

exports.User = function (socket) {
  ...
};

Then rename the file to user.js. Assuming it's in the root directory of your main script, you can include it like this:

然后将文件重命名为user.js。假设它位于主脚本的根目录中,您可以将其包括如下:

var user = require('./user');
var someUser = new user.User();

That's the quick and dirty version. Read about CommonJS Modules if you'd like to learn more.

这是快速而肮脏的版本。如果您想了解更多,请阅读CommonJS模块。

#4


5  

Another way in addition to the ones provided here for ES6

除了这里为ES6提供的其他方法

module.exports = class TEST{
    constructor(size) {
        this.map = new MAp();
        this.size = size;

    }

    get(key) {
        return this.map.get(key);
    }

    length() {
        return this.map.size;
    }    

}

and include the same as

并包含相同的as

var TEST= require('./TEST');
var test = new TEST(1);

#5


1  

If you append this to user.js:

如果你把这个附加到用户。js:

exports.User = User;

then in server.js you can do:

然后在服务器。js你能做什么:

var userFile = require('./user.js');
var User = userFile.User;

http://nodejs.org/docs/v0.4.10/api/globals.html#require

http://nodejs.org/docs/v0.4.10/api/globals.html要求

Another way is:

另一种方法是:

global.User = User;

then this would be enough in server.js:

这样在服务器上就足够了。

require('./user.js');