This question has already been answered:

As a workout, I wrote 2 constructors with the inheritance of the second from the first. When I try to access the properties of constructor 1 from constructor 2, I get undefined .

How to interact with properties from another constructor?

 // конструктор 1 function Construct1() { // свойства this.constructor1 = 'Construct 1'; this.property1 = 'property 1'; } // ΠΌΠ΅Ρ‚ΠΎΠ΄ 1 Construct1.prototype.method1 = function() { return `${this.constructor1} -> method 1 -> ${this.property1}`; } let construct1 = new Construct1(); // ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° ΠΌΠ΅Ρ‚ΠΎΠ΄Π° 1 console.log(construct1.method1()); // 'Construct 1 -> method 1 -> property 1' // конструктор 2 function Construct2() { // свойства this.constructor2 = 'Construct 2'; this.property2 = 'property 2'; } // наслСдованиС конструктора 2 ΠΎΡ‚ конструктора 1 Construct2.prototype = Object.create(Construct1.prototype); Construct2.prototype.constructor = Construct2; // ΠΌΠ΅Ρ‚ΠΎΠ΄ 2 Construct1.prototype.method2 = function() { return `${this.constructor2} -> method 2 -> ${this.property2}`; } // ΠΌΠ΅Ρ‚ΠΎΠ΄ 3 с наслСдованиСм ΠΌΠ΅Ρ‚ΠΎΠ΄Π° 1 Construct1.prototype.method3 = function() { return this.method1(); } let construct2 = new Construct2(); // ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° ΠΌΠ΅Ρ‚ΠΎΠ΄Π° 2 ΠΈ 3 console.log(construct2.method2()); // 'Construct 2 -> method 2 -> property 2' console.log(construct2.method3()); // 'undefined -> method 1 -> undefined' 

Reported as a duplicate at Grundy. javascript May 9 at 13:01 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    As a workout, I wrote 2 constructors with the inheritance of the second from the first

    This is not exactly the case.

     Construct2.prototype = Object.create(Construct1.prototype); 

    Here is a record of the prototype from Construct1 to the prototype Construct2 , but these are just prototypes. In your example, properties are created separately for each instance, therefore property1 will be created only by calling Constructor1 .

    In order to inherit another Holy Island, you need to call the constructor

     function Construct2() { Construct1.apply(this); // ΠΏΠ΅Ρ€Π΅Π΄Π°Π΅ΠΌ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ экзСмпляр Π² Ρ€ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ класс // свойства this.constructor2 = 'Construct 2'; this.property2 = 'property 2'; } 

    It is written here more fully.