What is the meaning of require('events').EventEmitter.call(this)

4,306 views
Skip to first unread message

Liu Yongjian

unread,
Jun 10, 2011, 5:29:35 AM6/10/11
to nod...@googlegroups.com
I find  the code snippet in the document 


var util = require("util");
var events = require("events");

function MyStream() {
    events.EventEmitter.call(this);  // What is the meaning here ? It seams that it is equivalent to `new MyStream`  
}

util.inherits(MyStream, events.EventEmitter);

var steam = new MyStream();


What doesn't the document  give the detail about this ???


Why ? 
Why ? 



Linus G Thiel

unread,
Jun 10, 2011, 8:56:47 AM6/10/11
to nod...@googlegroups.com
It means, "in the constructor for MyStream, call the constructor of
EventEmitter". Together with util.inherits -
http://nodejs.org/docs/v0.4.8/api/util.html#util.inherits - MyStream
will be an EventEmitter.

> --
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en.
>

--
Linus G Thiel
Hansson & Larsson
http://hanssonlarsson.se/
+46 709 89 03 85

Liu Yongjian

unread,
Jun 10, 2011, 10:07:22 AM6/10/11
to nod...@googlegroups.com
Hi Linus G
    Are you sure the function `EventEmitter.call` is the constructor of EventEmitter ? 
    What operation does it do?
    What is the purpose of calling `EventEmitter.call` in the constructor of MyStream ?

    Could you explain why ?

  Thank you very much 

Bradley Meck

unread,
Jun 10, 2011, 10:20:56 AM6/10/11
to nod...@googlegroups.com
Using EventEmitter.call on an object will do the setup of instance methods / properties (not inherited) of an EventEmitter. It is similar in purpose to super(...) in Java or base(...) in C#, but it is not implicit in Javascript. Because of this, we must manually call it ourselves. As for the talk about util.inherits, this will make the MyStream function inherit from another prototyped function so that instanceof works (side note: javascript only allows single inheritance). Due to how the new operator works, if we have the this variable in a function set to an instanceof EventEmitter, and call EventEmitter.call it look for all intents and purposes as if EventEmitter's constructor is being called on our MyStream object.

Isaac Schlueter

unread,
Jun 10, 2011, 11:37:15 AM6/10/11
to nod...@googlegroups.com
Another key point:

"Classes" in JavaScript are just functions with a "prototype" property
that's been dressed up. The "call" method on a Function object
executes the function in the this-context of the supplied object.

fn.call(obj, 1, 2, 3)
// is kinda like
obj.fn(1, 2, 3)

So, doing `ParentClass.call(this, x, y, z)` (or the more general:
ParentClass.apply(this, arguments)) is like saying "Apply the parent
class constructor to this object, as if we'd done `new ParentClass(x,
y, z)`, but do it to my object instead of creating a new one."

function Point2D (x, y) {
this.x = x
thix.y = y
}
Point2D.prototype =
{ distance: function (p) {
return Math.pow(Math.pow(p.x - this.x, 2) + Math.pow(p.y - this.y, 2), 0.5)
}
}

function Point3D (x, y, z) {
// call the parent ctor
Point2D.call(this, x, y)
this.z = z
}

// extend the prototype of the parent
Point3D.prototype = Object.create(Point2D.prototype, { constructor: {
value: Point3D }})

// override the distance method to make it 3d
Point3D.prototype.distance = function (p) {
return Math.pow(Math.pow(p.x - this.x, 2) + Math.pow(p.y - this.y,
2) + Math.pow(p.z - this.z, 2), 0.5)
}

> --
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/nodejs/-/udKTIwlQ0i0J.

dhruvbird

unread,
Jun 11, 2011, 1:26:47 AM6/11/11
to nodejs
Doesn't EventEmiter currently still work properly event w/o the call
to its c'tor (if you have inherited from it that is)? If so, why is
the call required?

Regards,
-Dhruv.

On Jun 10, 5:56 pm, Linus G Thiel <li...@hanssonlarsson.se> wrote:
> It means, "in the constructor for MyStream, call the constructor of
> EventEmitter". Together with util.inherits -http://nodejs.org/docs/v0.4.8/api/util.html#util.inherits- MyStream
> will be an EventEmitter.
>
>
>
>
>
>
>
>
>
> On Fri, Jun 10, 2011 at 11:29, Liu Yongjian <ggd...@gmail.com> wrote:
> > I find  the code snippet in the document
>
> > var util = require("util");
> > var events = require("events");
> > function MyStream() {
> >     events.EventEmitter.call(this);  // What is the meaning here ? It seams
> > that it is equivalent to `new MyStream`
> > }
> > util.inherits(MyStream, events.EventEmitter);
> > var steam = new MyStream();
>
> > What doesn't the document  give the detail about this ???
>
> > Why ?
> > Why ?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nodejs" group.
> > To post to this group, send email to nod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nodejs+un...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/nodejs?hl=en.
>
> --
> Linus G Thiel
> Hansson & Larssonhttp://hanssonlarsson.se/

Isaac Schlueter

unread,
Jun 11, 2011, 2:05:54 AM6/11/11
to nod...@googlegroups.com
On Fri, Jun 10, 2011 at 22:26, dhruvbird <dhru...@gmail.com> wrote:
> Doesn't EventEmiter currently still work properly event w/o the call
> to its c'tor (if you have inherited from it that is)? If so, why is
> the call required?

Even though it might not do anything today, if any functionality ever
*is* added to the EventEmitter ctor, you probably want it. So, it's a
good practice regardless.

dhruvbird

unread,
Jun 12, 2011, 12:01:04 AM6/12/11
to nodejs


On Jun 11, 11:05 am, Isaac Schlueter <i...@izs.me> wrote:
> On Fri, Jun 10, 2011 at 22:26, dhruvbird <dhruvb...@gmail.com> wrote:
> > Doesn't EventEmiter currently still work properly event w/o the call
> > to its c'tor (if you have inherited from it that is)? If so, why is
> > the call required?
>
> Even though it might not do anything today, if any functionality ever
> *is* added to the EventEmitter ctor, you probably want it.  So, it's a
> good practice regardless.

okay, thanks!!

follow up question: so why does eventemitter currently have stuff like
if (!this._events) everywhere if it expects people to init() it
properly? Asking since it thought it was pretty cool of a class to be
permissive this way.

Regards,
-Dhruv.

Evgeny Reznichenko

unread,
Jun 10, 2011, 6:13:03 AM6/10/11
to nod...@googlegroups.com
It is common practice in OOP js.

You call the constructor of the parent class in its constructor with a pointer to itself, thereby extending a parent's properties
.

example:
function A () { 
console.log('a constructor'); 
this.a = 'a' 
};

function B () 
A.call(this); 
console.log('b constructor'); 
this.b = 'b' 
};

var b = new B();
=> a constructor
=> b constructor
b.a => 'a';
b.b => 'b';

2011/6/10 Liu Yongjian <ggd...@gmail.com>
Reply all
Reply to author
Forward
0 new messages