A note on how newoperator works :
Foo = function(){};
Foo.prototype;
var bar = new Foo();
What happens on the last line, a new object is created that inherits from Foo.prototype, and then the code in the function Foois executed with thisthis new object. Then the line is initialized with this new object.
There are several drawbacks to your inheritance model, and you have discovered one of them. All the flaws are related to this line of code:
Person.prototype = new Emitter();
, , Person, - Emitter ( Emitter, ).
p1.dist1 dist1 . p1, , dist1, . p1.[[Prototype]], Person.prototype, dist1, Person.prototype ( Emitter, ).
p1.dist , p1 p1.
p2, p2.dist1, Emitter, p2.dist2 p2.
:
function Emitter() {
this.dis1 = this;
};
function Person() {
Emitter.call(this);
this.dis2 = this;
};
Person.prototype = Object.create(Emitter.prototype);
var p1 = new Person();
var p2 = new Person();
p1.dist1 === p1 p1.dist2 === p1, p2.dist1 === p2 p2.dist2 === p2. , , Emitter , Person. , Emitter this.dis1 = this, this p1 p2.
, , :
- , Emitter.prottoype, p1 p2
p1 instanceof Emitter .
, MDN.