There is a code:

<script> function base(x){ this.x = x; } function derived(y){ this.y = y; } </script> 

How can I declare a base class as a prototype of the class derived , if they are created using a constructor?

  • Are you still reading a book? :) which chapter? - Grundy
  • I already do the first experiments. - perfect

1 answer 1

In order to link the two classes you need to set the prototype property

 function Base(x){ this.x = x; } function Derived(y){ this.y = y; } Derived.prototype = Object.create(Base.prototype); 

To add inherited properties to the created object, call the base constructor in the heir

 function Derived(x, y){ Base.call(this, x); this.y = y; } 

Various approaches to inheritance can be seen in the help.

  • I thank, that is necessary - perfect