Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer my own question:</p> <pre class="lang-js prettyprint-override"><code>/* 1) */ p_constructor.prototype = new Superclass(); </code></pre> <p>Like I said, it's not very wise to use this because it actually creates the whole Superclass object. The whole point of prototypal inheritance is to share the prototype objects so redundant function (and sometimes object) definitions can be avoided.</p> <pre class="lang-js prettyprint-override"><code>/* 2) */ p_constructor.prototype = Superclass.prototype; </code></pre> <p>This would be OK if you could be sure that the constructor's prototype would never be modified by third party. Suppose somebody who uses your class wants to add additional functionality to its prototype. Changing the p_constructor's prototype would directly influence the superclass' prototype and could introduce errors in the superclass' behaviour. So the next line of code is used instead:</p> <pre class="lang-js prettyprint-override"><code>/* 3) */ p_constructor.prototype = Object.create(Superclass.prototype); </code></pre> <p>This creates a new object (instance) whose prototype (actually the <code>__proto__</code> variable) is set to Superclass' prototype. Any changes to <code>p_constructor.prototype</code> now makes no changes to the Superclass' behaviour.</p> <pre class="lang-js prettyprint-override"><code>/* 4) */ p_constructor.prototype = Object.create(Superclass); </code></pre> <p>The upper line works, but makes no sense because it only sets the prototype to the Superclass' constructor.</p> <p>Also, the following line was confusing to me as well:</p> <pre class="lang-js prettyprint-override"><code>p_constructor.prototype.constructor = p_constructor; </code></pre> <p>It makes no difference in the p_constructor's behaviour, it only makes the reference to the constructor available in the instantiated object. A neat trick is to add the superclass to the prototype as well:</p> <pre class="lang-js prettyprint-override"><code>p_constructor.prototype.superclass = Superclass; </code></pre> <p>And then the following piece of code would work:</p> <pre class="lang-js prettyprint-override"><code>function Constructor(p_params) { // call constructor of the superclass this.superclass(p_params); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload