Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your first proof:</p> <pre><code>function a () {}; function b () {}; a.prototype === b.prototype; // false </code></pre> <p>You're hitting on the fact that the <code>prototype</code> isn't <code>Object</code> -- it's not the base class. It's an object, which is in turn extended, prototypically, with the methods and properties of <code>Object</code>.<br> And any test of <code>{} === {};</code> is going to return false, <strong><em>always</em></strong>, unless you're testing two pointers to the same object:</p> <pre><code>var a = {}, b = a; a === b; // true </code></pre> <p>Your inheritance trainwreck afterwards points directly to missing this concept, as you've set all three functions to point to the same object (originally to <code>Object</code> itself, and then to <code>Obj1</code>'s prototype object).<br> A <code>prototype</code> is <strong><em>live</em></strong>, any change to the prototype, after object construction, will reflect in the objects containing a pointer to that object.</p> <p>You've given <code>Obj1</code>, <code>Obj2</code> and <code>Obj3</code> a pointer to the exact same object.<br> Ergo, any modifications to that prototype object they all share will reflect in all of their constructed objects, as inheritance-resolution happens in real-time, when you attempt to access a property (rather than living in some static, pre-compiled, GOTO state).</p> <p>If you wanted the ability to subclass, you could do something like this:</p> <pre><code>var A = function () {}; A.prototype.foo = function () {}; var a = new A(); var B = function () {}; B.prototype = a; // or new A(); -- an object which can live-lookup the prototype of A // problem is, `constructor` on the prototype will now point to `A`, so... B.prototype.constructor = B; // now b instanceof B should work in tests B.bar = function () {}; var b = new B(); b.foo(); // A.prototype.foo b.bar(); // B.prototype.bar </code></pre> <p>But really, you're going to drive yourself crazy if you try to extend this too far.<br> A saner path to follow in JS is component-based object construction, and dependency-injection.</p>
    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.
    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