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?
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?
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.
Source: https://ru.stackoverflow.com/questions/516300/
All Articles