Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>JavaSript does not have true multiple inheritance. You can inherit from only one prototype and then copy the rest of the properties that you want. You can test this by using the <code>instanceof</code> operator.</p> <p>After fixing the mis-spellings, your demo works, but in actuality, you are not truly inheriting. To do true JS inheritance:</p> <pre><code>function A(){} function B(){} B.prototype = new A; b = new B; console.log(b instanceof A, b instanceof B); //-&gt; true, true </code></pre> <p><a href="https://stackoverflow.com/questions/6887828/does-javascript-support-multiple-inheritance-like-c">See also</a></p> <p>More about <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance" rel="nofollow noreferrer">JS inheriance on MDN</a></p> <h2>Quasi-Multiple Inheritance</h2> <pre><code>function ctorX() { this.messageX = "this is X Message"; this.alertX = function() { console.log(this.messageX); }; } function ctorY() { this.messageY = "this is Y Message"; this.alertY = function() { console.log(this.messageY); }; } function ctorZ() { ctorX.call(this); // This is the quasi-multiple inheritance this.messageZ = "this is Z Message"; this.alertZ = function() { console.log(this.messageZ); }; } ctorZ.prototype = new ctorY; // This is the actual inheritance var objz = new ctorZ(); objz.alertZ(); objz.alertY(); objz.alertX(); console.assert(objz instanceof ctorZ, 'objz is not instance of ctorZ'); console.assert(objz instanceof ctorY, 'objz is not instance of ctorY'); console.assert(objz instanceof ctorX, 'objz is not instance of ctorX'); //The last assert will fail since there is no true multiple inheritance </code></pre> <h3><a href="http://jsfiddle.net/kaleb/ZmHLm/" rel="nofollow noreferrer">Demo of Quasi-Multiple Inheritance</a></h3> <h2>Avoid Calling the Super Constructor</h2> <p><a href="https://stackoverflow.com/users/1641941/hmr">HMR</a> brought up the point that in some instances, a user wants to inherit from a particular constructor, but the super-constructor requires parameters and will fail w/o them. The way to bypass this is to create a proxy constructor:</p> <pre><code>function C(x){if(!x) throw new Error;} function D(){} function proxyCtor(){/*should be noop*/} proxyCtor.prototype = C.prototype; D.prototype = new proxyCtor; var d = new D; console.assert(d instanceof C, 'c is not instance of D'); // will err if incorrect, which it's not </code></pre> <h3><a href="http://jsfiddle.net/kaleb/ZmHLm/3/" rel="nofollow noreferrer">Demo</a></h3>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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