node.js - Different ways of extending class in Javascript -
i'm reading this article on extending eventemitter class in node , there's small part don't understand:
door.prototype.__proto__ = events.eventemitter.prototype;
i've looked several articles on how supposed work don't understand it. know alternatively, use util.inherits
i'm trying understand happens in line above.
i'd know if doing have same result using __proto__
:
door.prototype = object.create(eventemitter.prototype); //or door.prototype = new eventemitter(); // know calls constructor
so, difference these 3 methods, , how __proto__
work?
edit:
so looked @ links posted. understand prototype used build __proto__
used in lookup chain resolve methods. article linked says line copies eventemitter properties door object:
door.prototype.__proto__ = events.eventemitter.prototype;
now question, might not have been clear earlier is: if point of line above, copying properties of eventemitter door object, think same achieved doing:
door.prototype = new eventemitter();
this objects have access , why.
we need understand __proto__
vs prototype. refer question , answer understand two. __proto__ vs. prototype in javascript
the line of code looking @ constructor inheriting eventemitter prototype. creates chain system include methods multiple constructor functions.
door.prototype.open = function(){ ... open something... }; door.prototype.__proto__ = events.eventemitter.prototype; var mydoor = new door;
the object mydoor have method open
, methods eventemitter prototype.
Comments
Post a Comment