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

Showing 1-9 of 9 messages
What is the meaning of require('events').EventEmitter.call(this) 爱国者 6/10/11 2:29 AM
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 ? 



Re: [nodejs] What is the meaning of require('events').EventEmitter.call(this) Linus G Thiel 6/10/11 5:56 AM
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

Re: [nodejs] What is the meaning of require('events').EventEmitter.call(this) 爱国者 6/10/11 7:07 AM
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 
Re: [nodejs] What is the meaning of require('events').EventEmitter.call(this) Bradley Meck 6/10/11 7:20 AM
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.
Re: [nodejs] What is the meaning of require('events').EventEmitter.call(this) Isaac Schlueter 6/10/11 8:37 AM
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.

> 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.
>

Re: What is the meaning of require('events').EventEmitter.call(this) dhruvbird 6/10/11 10:26 PM
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/
Re: [nodejs] Re: What is the meaning of require('events').EventEmitter.call(this) Isaac Schlueter 6/10/11 11:05 PM
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.

Re: What is the meaning of require('events').EventEmitter.call(this) dhruvbird 6/11/11 9:01 PM


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.
Re: [nodejs] What is the meaning of require('events').EventEmitter.call(this) Evgeny Reznichenko 6/10/11 3:13 AM
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>


--
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.