JavaScript Inheritance (infinite loop when using the superclass function)

I am looking at Tuts + OO JavaScript training: the way they do prototypical inheritance is as follows:

Employee.prototype = Object.create(Person.prototype);

But why can't I just (without creating an object)

Employee.prototype = Person.prototype;

The second will give an infinite loop: http://jsfiddle.net/VMqSy/

Why? The prototype logging console tells me that 1 sec Object.create()will give Employee.prototype 1 another level __proto__, which is Person.prototype, I think,

0
source share
1 answer

If you do this (as shown in the fiddle)

Employee.prototype = Person.prototype;

Employee Person, ( ).

, , Employee, Person. , .


Employee.prototype = Object.create(Person.prototype);

,

Employee.prototype = {};
Employee.prototype.__proto__ = Person.prototype

Object.create ( __proto__)

Employee.prototype = new Person();

, Person Employee.prototype, Object.create .


, Employee.prototype.sayHi Person.prototype.sayHi, Employee.prototype.sayHi Person.prototype.sayHi, , , ( Person.prototype.sayHi Employee.prototype.sayHi).

Employee.prototype === Person.prototype, , , Employee.prototype.sayHi === Person.prototype.sayHi.

+2

Source: https://habr.com/ru/post/1537030/


All Articles