I'm reading this article on extending the EventEmitter class in node and there's a small part that I don't understand:
Door.prototype.__proto__ = events.EventEmitter.prototype;
I've looked up several articles on how this is supposed to work but really don't understand it. I know that alternatively, I could also use util.inherits
but I'm trying to understand what happens in the line above.
I'd also like to know if doing this would have the same result as using __proto__
:
Door.prototype = Object.create(EventEmitter.prototype);
//or
Door.prototype = new EventEmitter(); // I know this also calls constructor
So, what is the difference about these three methods, and how does __proto__
work?
EDIT:
So I looked at the links that were posted. I understand that prototype is basically used to build __proto__
which is then used in the lookup chain to resolve methods. The article I linked to also says that this line copies all the EventEmitter properties to the door object:
Door.prototype.__proto__ = events.EventEmitter.prototype;
Now my question, which might not have been clear earlier is: if the point of the line above, is copying the properties of the EventEmitter to the door object, I think the same could be achieved by doing:
Door.prototype = new EventEmitter();