admin管理员组文章数量:1325236
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
1 Answer
Reset to default 7result 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();
本文标签: javascriptNode js and new when using requireStack Overflow
版权声明:本文标题:javascript - Node js and new when using require - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742166743a2425964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论