Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It <em>sounds</em> like you actually want to link instances of <code>Man</code> to an instance of <code>Person</code> that retains any properties added in its constructor, in which case you might really want something like this:</p> <pre><code>function Person(a, b) { this.a = a; this.b = b; } function Man() {} Man.prototype = new Person("val1", "val2"); var m = new Man(); console.log(m.a); </code></pre> <p>...which prints <code>val1</code>.</p> <h2>Additional Ramblings</h2> <p>The purpose of <code>inherit</code> is to create an object from a function whose <code>prototype</code> is that of the given super class (without explicitly using <code>new</code>), which is exactly what it does. Therefore, the following prints <code>string</code>:</p> <pre><code>function Person() {} Person.prototype.test = "string"; function Man() {} function inherit(superClass) { function F() {} F.prototype = superClass.prototype; return new F; } var t = inherit(Person); console.log(t.test); </code></pre> <p>But you generally want to assign the returned object to another function's prototype:</p> <pre><code>Man.prototype = inherit(Person); var m = new Man(); console.log(m.test); </code></pre> <p>...so that <code>m.test</code> also prints <code>string</code>, which means that objects created using <code>Man</code> are linked to <code>Person</code>'s <code>prototype</code>).</p> <p>Note that <code>Man.prototype.prototype</code> is <code>undefined</code> and -- this is the important part -- also meaningless. Functions have prototypes. Other objects (such as <code>Man.prototype</code>) do not. The <code>prototype</code> property is not magical in any other context. Assigning a value to a random object's <code>prototype</code> property doesn't do anything special. It's just another property.</p> <p>Note also that the thing we returned from <code>inherit</code> is linked to <code>Person</code> by way of its <code>prototype</code> and has no access to any properties added to <em>instances</em> of <code>Person</code>.</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. 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.
    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