Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Jon is right. Here's a way to fix it. Instead of setting StrangeArray.prototype to Array.prototype, this will let you set StrangeArray.prototype to a new instance of Array, so it inherits Array.prototype's properties (without calling Array's constructor).</p> <pre><code>Object.prototype.inherit = function(baseConstructor) { var tmp = Function(); tmp.prototype = baseConstructor.prototype; this.prototype = new tmp(); this.prototype.constructor = this; }; </code></pre> <hr> <p>Edit:</p> <pre><code>this.prototype = new baseConstructor(); </code></pre> <p>works in this example, but is not good in more complex programs. If the base constructor does some initialization, such as creating DOM elements, incrementing a count, or connecting to a server, then all that initialization would take place when the script loads, instead of when the child object is instantiated.</p> <p>Another way to deal with this is to differentiate in the constructor whether it is being called for inheritance (to assign to a prototype) or just to instantiate, and then not do the initialization stuff if it is being called for inheritance. But I prefer to just not call the constructor when inheriting, and so use an empty function for the constructor when inheriting.</p> <p>I'm not very good at explaining this stuff. I recommend Crockford's site and also these <a href="http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html" rel="nofollow noreferrer">two articles</a> on <a href="http://ejohn.org/blog/simple-javascript-inheritance/" rel="nofollow noreferrer">Javascript inheritance</a>.</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