You need to create an instance.
var myAuthObj = new Auth();
myAuthObj.authenticate(...);
Methods on the prototype are "instance" methods. They are directly callable on an instance of the object.
You can create "static" methods too (in a different way which are really just plain functions assigned to a namespace object), but they cannot use this
or your inherited object because those are put into place only when you instantiate an actual object with new
and your constructor.
You also need to move your util.inherits()
in front of your prototype assignment. The util.inherits()
statement replaces the prototype so if you do it afterwards, you wipe out the things you just assigned to the prototype. And, you should call the constructor of your parent object too.
function Auth() {
EventEmitter.call(this);
console.log('Created!')
}
util.inherits(Auth, EventEmitter);
Auth.prototype.authenticate = function () {
// do stuff
this.emit('completed')
}
So, three things to fix:
- Move
util.inherits()
before you assign anything to Auth.prototype
.
- Add
EventEmitter.call(this)
to your constructor to properly initialize the base object
- Construct an instance of your Auth object with
new Auth()
and call methods on that instance.