I'm learning Node.js and have some confusion about customized EventEmitter. Here's the code:
var events = require("events");
function MyEmitter (name){
this.name = name;
// events.EventEmitter.call(this);
this.emitEvent = function(){
this.emit("Event1");
}
}
//MyEmitter.prototype.__proto__ = events.EventEmitter.prototype;
MyEmitter.prototype = events.EventEmitter.prototype;
function foo(){
console.log("callback: " + this.name);
}
var obj = new MyEmitter("MyEmitter");
obj.on("Event1", foo);
obj.emitEvent();
There's two similar lines to inherit from EventEmitter;
//MyEmitter.prototype.__proto__ = events.EventEmitter.prototype;
MyEmitter.prototype = events.EventEmitter.prototype;
It seems both of these two expressions will work. And I saw "The proto property is deprecated" from here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
Besides, if I comment out the
// events.EventEmitter.call(this);
The code will also work.
I heard that call() is used as a constructor of EventEmitter,
//MyEmitter.prototype.__proto__ = events.EventEmitter.prototype;
and this line is used to copy all of the EventEmitter properties to the Door object.
So why do we have to copy the properties twice?
Could anyone tell me what's the difference between these expression?