Excellent thank you
On Wednesday, 20 March 2013 21:58:21 UTC-7, Postlethwaite wrote:
>
> I have a little code question for you.
>
> *code:*
>
> var EventEmitter = require('events').EventEmitter
>
>
>
> function buildEmitters(name) {
>
> var self = new EventEmitter
>
> function sayWhat () {
> self.emit('what')
> }
>
> self.on('what', function () {
> console.log(name + ' says what')
> })
>
>
> function addGroup (group) {
> self.on(group, function () {
> console.log(name + ' was called by group, what')
> })
> }
>
> function callGroup (group) {
> self.emit(group, name)
> }
>
> self.sayWhat = sayWhat
> self.addGroup = addGroup
> self.callGroup = callGroup
>
> return self
>
> }
>
>
>
> var A = buildEmitters('grep')
> , B = buildEmitters('cat')
> , C = buildEmitters('tar')
>
>
> A.addGroup('wheel')
> B.addGroup('wheel')
> C.addGroup('wheel')
>
>
> A.sayWhat()
> B.sayWhat()
>
> C.callGroup('wheel')
>
>
> which outputs:
>
> grep says what
> cat says what
> tar was called by group, what
>
> I would like it to say:
>
> grep says what
> cat says what
> tar was called by group, what
> cat was called by group, what
> grep was called by group, what
>
> I know I can use the
>
> if (!(this instanceof buildEmitters)) return new buildEmitters()
> EventEmitter.call(this)
>
>
> along with the
>
> buildEmitters.prototype = new EventEmitter
>
>
> and in the addGroup method:
>
> buildEmitters.prototype.on(group, callback)
>
> and in the callGroup method:
>
> buildEmitters.prototype.emit(group)
>
>
> pattern. It works, and it's used everywhere.
>
> *problem:*
> But I am attempting to write code without inheritance and prototype calls.
> Zero.
> Just clean and simple little objects.
>
> *question:*
> Is it possible to get 'events' to propagate between the objects that *
> buildEmitters* creates? Any patterns, hacks or blatantly obvious
> workarounds that I am missing?
>
> Thanks!
>
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.