Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript: X.prototype.x adds x to base class, too
    primarykey
    data
    text
    <p>I'm fluent in C++ so I have a good background in OOP, but I'm new to JavaScript and I'm trying to understand how to properly implement polymorphism in JavaScript. I have read a number of tutorials and StackOverflow questions related to this topic, but none has explained in detail how to correctly <em>inherit</em> from a base class and also <em>add new members</em> to the child class.</p> <pre><code>function Obj1() {} function Obj2() {} function Obj3() {} Obj2.prototype === Obj3.prototype; // false </code></pre> <p><strong>Curiosity #1:</strong> If both <code>Obj2</code> and <code>Obj3</code> (and, for that matter, <code>Obj1</code>) inherit from <code>Object</code>, why are their prototypes not identical? Did <code>Obj2</code> and <code>Obj3</code> each receive its own <em>copy</em> of <code>Object</code>?</p> <pre><code>Obj2.prototype = Object; Obj2.prototype = Object; Obj2.prototype === Obj3.prototype; // true Obj2.prototype = Obj1; Obj3.prototype = Obj1; Obj2.prototype === Obj3.prototype; // true </code></pre> <p><strong>Curiosity #2:</strong> In both cases, <code>Obj2</code> and <code>Obj3</code> now share the same prototype. What has changed?</p> <pre><code>Obj2.prototype.pi = function() { return 3.14159; } Obj2.prototype.pi(); // 3.14159 Obj3.prototype.pi(); // 3.14159 Obj1.pi(); // 3.14159 </code></pre> <p><strong>The Real Question:</strong> Changing the prototype of <code>Obj2</code> also changes the prototype of <code>Obj3</code> and the function <code>Obj1</code> itself. This is all quite shocking to this C++ programmer, even though it's exactly what I expected from the above syntax: after all, since the prototypes of <code>Obj2</code> and <code>Obj3</code> have both been equated with <code>Obj1</code>, a change to one will produce an identical change to the other two. Nevertheless, I want to use <code>Obj1</code> as a base class for both <code>Obj2</code> and <code>Obj3</code> <em>and</em> insert a new function on <code>Obj2</code> that is <em>not</em> shared in any way by <code>Obj1</code> or <code>Obj3</code>. How do I do that?</p> <p><strong>Further curiosities</strong> arise when these objects are instantiated:</p> <pre><code>var x = new Obj2(); x.pi(); // 3.14159 var y = new Obj3(); y.pi(); // 3.14159 var z = new Obj1(); z.pi(); // Error: pi() not defined on z. </code></pre> <p><code>Obj2</code> and <code>Obj3</code> both have a function <code>pi()</code>, which implies that they still share a <em>base class</em> that contains a function <code>pi()</code>. This base class is none other than <code>Obj1</code>&mdash;but an instance of <code>Obj1</code> does <em>not</em> contain <code>pi()</code>, despite the evidence to the contrary!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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