0
function Auth() {
  console.log('Created!')
}
Auth.prototype.authenticate = function () {
  // do stuff
  this.emit('completed')
}
util.inherits(Auth, EventEmitter)

How can i call Auth.emit('someEvent') in other functions? For example:

function someOther () {
  //do stuff
  Auth.emit('event')
}

It throws an error:

Auth.emit is not a function

Src
  • 5,252
  • 5
  • 28
  • 56
  • 1
    You need to create an instance of `Auth` first. Or not use prototype functions. – zerkms Jun 28 '16 at 23:34
  • @zerkms , ok, but it is module (i'm leting user create new instance of it when requiring module), so i can move this someOther function to the class. BUT, i need to call function BEFORE new instane of that class is created. How can i do that? Is it even possible? – Src Jun 28 '16 at 23:37
  • Well, it's not possible to call an instance method without an instance. You need to reconsider what and how you need to do. – zerkms Jun 28 '16 at 23:38
  • @zerkms , any ideas (http://pastebin.com/V84MH4cR) ? – Src Jun 28 '16 at 23:41
  • Sure: simply don't invoke instance methods if you don't have an instance. – zerkms Jun 28 '16 at 23:44
  • It might be worth whole reading up a little more on the difference between standard properties and prototypes – JD Byrnes Jun 28 '16 at 23:47

1 Answers1

1

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:

  1. Move util.inherits() before you assign anything to Auth.prototype.
  2. Add EventEmitter.call(this) to your constructor to properly initialize the base object
  3. Construct an instance of your Auth object with new Auth() and call methods on that instance.
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Also he needs to put the `util.inherits` call in front of the assignment to `Auth.prototype.authenticate`. – Bergi Jun 29 '16 at 01:30
  • @Bergi - Good catch. Incorporated that too. – jfriend00 Jun 29 '16 at 01:55
  • And while we're at it: [It's not strictly necessary](http://stackoverflow.com/a/22791445/1048572) but certainly best practice to call the super constructor :-) – Bergi Jun 29 '16 at 02:17
  • Thank you for reply, can you look at my module code? (http://pastebin.com/V84MH4cR) I understand that I need to make an instance, but what is the best way to do it in my module (I'm asking because of module.export was actually exporting newly created instance) – Src Jun 29 '16 at 16:02
  • @Src - You haven't disclosed any context for the code that is trying to use your `Auth` object so I don't know what else you're asking. We've explained that you need an instance in order to use it. Your module shows exporting an instance which is good, but you did not fix the `util.inherits()` issue or the base constructor issue I pointed out in my answer. Please read my answer and fix all three things mentioned there. – jfriend00 Jun 29 '16 at 16:22
  • @jfriend00 , yeah, i know about constructor, but that code i posted before your answer been submited. I am asking if it is normal to export already created instance, which i need to create in order to use prototype instance functions? (best practices...) – Src Jun 29 '16 at 19:58
  • @Src - Exporting only an already created instance means your module can only ever be used to create one and only one instance and anyone using your module gets the same instance. That is not technically wrong, but it limits the use of your module since different users of the module could not have their own instances. – jfriend00 Jun 29 '16 at 20:06
  • @jfriend00 , thank you, that is what i was asking for... do you have any ideas how i can avoid that? – Src Jun 29 '16 at 21:28
  • @Src - Yeah, export the constructor and let the caller create an instance. That's infinitely more flexible. – jfriend00 Jun 30 '16 at 04:54
  • @jfriend00 ,that is what i was doing, but then we come back to problem of using instance method... – Src Jun 30 '16 at 12:41
  • @Src - Not if the user of your object creates an instance. – jfriend00 Jun 30 '16 at 13:05
  • @jfriend00 , he woun't be able event to create an instance because of error in module, when you require it – Src Jun 30 '16 at 13:09