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

javascript - Node js and new when using require - Stack Overflow

programmeradmin0浏览0评论

I've been trying to sort out the include of other js files within node.js.

I've read all about the require function and other alternatives and decided to go with the require function (as the code will only ever be used on node.js, not in a browser).

In my code I'm using prototypes to create an 'object' which I later wish to create an instance off.

To get it to work I've been writing code like the following (lets call it vehicle.js):

var util = require('util');
var EventEmitter = require('events').EventEmitter;

module.exports = Vehicle;

util.inherits(Vehicle, EventEmitter);

function Vehicle(options) {
    EventEmitter.call(this);
    options = options || {};
    ...
}

Vehicle.prototype._doStartEvent = function(data) {
    this.emit('start', data);
};

Vehicle.prototype.testRun = function() {
    this._doStartEvent();
};

Then in my main js (lets call it server.js), I have the following:

var test = exports;
exports.Vehicle = require('./vehicle.js');

var remoteVehicle = new test.Vehicle({address: "192.168.1.3"});

remoteVehicle.on('start', function(d) {console.log('started');});

remoteVehicle.testRun();

Now this all works fine, but I do not have a good understanding of what is going on.

My main concern is the use of var test = exports; and then exports.Vehicle = require(...).

I tried just doing something like var vehicle = require(...).Vehicle and var vehicle = require(...), with the goal of just using new Vehicle or similar, but I couldn't get it to work.

Am I forced to use exports and if so why?

Please note I've been using the AR Drone project as an example, the above code is based on how they have done their modules internally. Refer to Client.js and index.js.

I've been trying to sort out the include of other js files within node.js.

I've read all about the require function and other alternatives and decided to go with the require function (as the code will only ever be used on node.js, not in a browser).

In my code I'm using prototypes to create an 'object' which I later wish to create an instance off.

To get it to work I've been writing code like the following (lets call it vehicle.js):

var util = require('util');
var EventEmitter = require('events').EventEmitter;

module.exports = Vehicle;

util.inherits(Vehicle, EventEmitter);

function Vehicle(options) {
    EventEmitter.call(this);
    options = options || {};
    ...
}

Vehicle.prototype._doStartEvent = function(data) {
    this.emit('start', data);
};

Vehicle.prototype.testRun = function() {
    this._doStartEvent();
};

Then in my main js (lets call it server.js), I have the following:

var test = exports;
exports.Vehicle = require('./vehicle.js');

var remoteVehicle = new test.Vehicle({address: "192.168.1.3"});

remoteVehicle.on('start', function(d) {console.log('started');});

remoteVehicle.testRun();

Now this all works fine, but I do not have a good understanding of what is going on.

My main concern is the use of var test = exports; and then exports.Vehicle = require(...).

I tried just doing something like var vehicle = require(...).Vehicle and var vehicle = require(...), with the goal of just using new Vehicle or similar, but I couldn't get it to work.

Am I forced to use exports and if so why?

Please note I've been using the AR Drone project as an example, the above code is based on how they have done their modules internally. Refer to Client.js and index.js.

Share Improve this question edited Oct 1, 2016 at 20:49 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Jan 3, 2013 at 6:21 MetalskinMetalskin 4,2687 gold badges40 silver badges63 bronze badges 4
  • This Mastering Node eBook is good: visionmedia.github./masteringnode/book.html – Alex Commented Jan 3, 2013 at 9:16
  • @LeonidBeschastny How is this a duplicate? This question was asked first (this was asked 3rd Jan 2013, the one you claim it's a duplicate of was asked 11th July 2013). If you think they're duplicates then the other question is the duplicate, not this one. – Metalskin Commented May 14, 2015 at 23:08
  • @Metalskin sorry, I was confused by the title. I re-red your question more carefully, and I think I was mistaken about it being a duplicate. – Leonid Beschastny Commented May 15, 2015 at 8:01
  • @LeonidBeschastny no worries, no harm no foul :-) – Metalskin Commented May 15, 2015 at 10:32
Add a ment  | 

1 Answer 1

Reset to default 7

result of require is a reference to exports object which is function in your case. Just assign to a variable with the same name as class (or any other) and use as a parameter to new

var Vehicle = require('./vehicle.js');

var remoteVehicle = new Vehicle({address: "192.168.1.3"});

remoteVehicle.on('start', function(d) {console.log('started');});

remoteVehicle.testRun();
发布评论

评论列表(0)

  1. 暂无评论