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

node.js - javascript: require('events').EventEmitter; - Stack Overflow

programmeradmin1浏览0评论

Why

var EventEmitter = require('events').EventEmitter;
var channel = new EventEmitter();

works, but

var EventEmitter = require('events');
var channel = new EventEmitter();

does not work! Actually, I have another totally different example,

var Currency = require('./currency)
var Cu = new Currency();

works, but

var Currency = require('./currency).Currency;
var Cu = new Currency();

does not work. Here is my currency.js:

function Currency(canadianDollar) {
    this.canadianDollar = canadianDollar;
}

module.exports = Currency;

Currency.prototype.cal = function(amount) {
    return amount * this.canadianDollar;
}

Why

var EventEmitter = require('events').EventEmitter;
var channel = new EventEmitter();

works, but

var EventEmitter = require('events');
var channel = new EventEmitter();

does not work! Actually, I have another totally different example,

var Currency = require('./currency)
var Cu = new Currency();

works, but

var Currency = require('./currency).Currency;
var Cu = new Currency();

does not work. Here is my currency.js:

function Currency(canadianDollar) {
    this.canadianDollar = canadianDollar;
}

module.exports = Currency;

Currency.prototype.cal = function(amount) {
    return amount * this.canadianDollar;
}
Share Improve this question asked Feb 4, 2014 at 16:04 BensonBenson 2752 gold badges6 silver badges12 bronze badges 3
  • Because they export different things. – SLaks Commented Feb 4, 2014 at 16:07
  • github./joyent/node/blob/v0.10.22/lib/events.js#L38 – SLaks Commented Feb 4, 2014 at 16:08
  • if you want yours to be like events, module.exports = {Currency:Currency}; – dandavis Commented Feb 4, 2014 at 16:08
Add a ment  | 

2 Answers 2

Reset to default 8

Because this is the way the API is written. A simplified example of "events" module would look like:

module.exports = {
    EventEmitter: function () {
        // ...
    }
};

In the above case require('events'); would return an Object containing EventEmitter, but require('events').EventEmitter would return the actual EventEmitter function you are likely interested in instantiating.

Thought it would be good to mention that the API designer could indeed export the EventEmitter function directly with module.exports = function () { ... }; however, they decided to leave space for other potentially useful properties of the "events" module.

Edit

Regarding module.exports = EventEmitter; in https://github./joyent/node/blob/master/lib/events.js, on the following lines you can find:

// Backwards-pat with node 0.10.x
EventEmitter.EventEmitter = EventEmitter;

I suppose that starting from version 0.11 you can run var Emitter = require('events');, but in 0.10.x you are stuck with require('events').EventEmitter.

require returns exactly what you put in module.exports object. So if you have module.exports = Currency; then require returns Currency and if you have exports.Currency = Currency require will return { Currency : Currency } object.

if you want this to work both ways, just do Currency.Currency = Currency; module.exports = Currency. in latest node releases they do like this with EventEmitter

发布评论

评论列表(0)

  1. 暂无评论