I have an object in a file and want to be able to require that file and then create new instances of the object at whim, but I've hit a snag. This seems so incredibly basic, what am I missing.
hat.js
function Hat(owner) {
this.owner = owner;
}
Hat.prototype.tip = function() {
console.log("and he (" + owner + ") tipped his hat, just like this");
}
exports.Hat = Hat;
node terminal
Attempt 1
> require('./hat.js');
> var mighty_duck = new Hat('Emilio');
ReferenceError: Hat is not defined
Attempt 2
> var Hat = require('./hat.js');
> var mighty_duck = new Hat('Emilio');
{ owner: 'Emilio' }
> mighty_duck.tip();
TypeError: Object #<Hat> has no method 'tip'
I, most unfortunately, left out what turned out to be the biggest problem bit. The fact that I was trying to use
util.inherits(Hat, EventEmitter);
So my hat.js would actually be
function Hat(owner) {
this.owner = owner;
}
Hat.prototype.tip = function() {
console.log("and he (" + owner + ") tipped his hat, just like this");
}
util.inherits(Hat, EventEmitter);
exports.Hat = Hat;
This is an issue, because, apparently it is important to have your inherits call before you start extending the prototype. The fix is simple, move the inherits call up a few lines
function Hat(owner) {
this.owner = owner;
}
util.inherits(Hat, EventEmitter);
Hat.prototype.tip = function() {
console.log("and he (" + owner + ") tipped his hat, just like this");
}
exports.Hat = Hat;
You are doing your require wrong:
var Hat = require('./hat.js').Hat;
Is what you want. When you do exports.Hat = Hat; you are exporting an object (exports) with a propery of Hat. So when you require './hat.js' you get the object, and need to access the Hat property.
From node repl:
> require('./hat.js');
{ Hat: [Function: Hat] }
> require('./hat.js').Hat;
[Function: Hat]
> var Hat = require('./hat.js').Hat;
undefined
> var mighty_duck = new Hat('Emilio');
undefined
> mighty_duck.tip();
and he (Emilio) tipped his hat, just like this
Of course that was causing an error since you said owner and not this.owner in tip(), but after I changed that it worked fine :)
If you want to just do
var Hat = require('./hat.js'),
hat = new Hat('Emilio');
Then change your export to:
module.exports = exports = Hat;
And it works:
> require('./hat.js');
[Function: Hat]
> var Hat = require('./hat.js');
undefined
> var hat = new Hat('Emilio');
undefined
> hat.tip();
and he (Emilio) tipped his hat, just like this
EDIT Cleaned up and inherits from EventEmitter:
var util = require('util'),
events = require('events');
var Hat = exports.Hat = function(owner) {
events.EventEmitter.call(this);
this.owner = owner;
}
util.inherits(Hat, events.EventEmitter);
Hat.prototype.tip = function() {
console.log("and he (" + this.owner + ") tipped his hat, just like this");
}
Have a look here: http://openmymind.net/2012/2/3/Node-Require-and-Exports/
you might try this instead:
function Hat(owner) {
this.owner = owner;
}
Hat.prototype.tip = function() {
console.log("and he (" + this.owner + ") tipped his hat, just like this");
}
module.exports = Hat;
and then sth. like this:
var hat = require('./hat.js');
var mighty_duck = new hat('Emilio');
mighty_duck.tip()