Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's because you write</p> <pre><code>SubClass.prototype = new Class("proto_class"); </code></pre> <p>you are creating a prototype of <strong>one instance</strong> of <code>Class</code>. What you want is to create a sub-class that inherits from its parent's prototype. As David Flanagan shows in his <a href="http://rads.stackoverflow.com/amzn/click/B002AWX8CI" rel="nofollow noreferrer">JavaScript: The Definitive Guide</a> (§&nbsp;9.5), you have to use a helper function to create a new object with a specified prototype:</p> <pre><code>function heir(p) { function f(){} // dummy constructor function f.prototype = p; // specify prototype object we want return new f(); // create and return new object } </code></pre> <p>(Crockford calls this function <code>Object.create</code>, after the so-called <a href="http://ecma262-5.com/ELS5_Section_15.htm#Section_15.2.3.5" rel="nofollow noreferrer">ES5 object constructor property</a>, but please don't do this, as this <a href="https://stackoverflow.com/questions/3075308/what-modernizer-scripts-exist-for-the-new-ecmascript-5-functions/3075818#3075818">can be misleading</a>.)</p> <p>Within the SubClass constructor, you have to <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call" rel="nofollow noreferrer">call</a> the Class constructor with <code>this</code> set to the current object:</p> <pre><code>function SubClass() { // call the parent's constructor with // `this` set to the current scope Class.call(this, "proto_class"); } </code></pre> <p>And last, but not least, you only reset <code>Class.asdf2</code> once, but not in the constructor function of <code>Class</code> or <code>SubClass</code>. So add <code>this.asdf2 = [];</code> to one of the constructors.</p> <p>The complete code now reads:</p> <pre><code>function heir(p) { function f(){} // dummy constructor function f.prototype = p; // specify prototype object we want return new f(); // create and return new object } function Class(asdf) { if (typeof asdf != 'undefined') this.asdf = asdf; } Class.prototype.asdf = "default_asdf"; Class.prototype.asdf2 = []; Class.prototype.change_asdf = function() { this.asdf = "changed_asdf"; this.asdf2.push("changed_asdf2"); } function SubClass() { // call the parent's constructor with // `this` set to the current scope Class.call(this, "proto_class"); this.asdf2 = []; } SubClass.prototype = heir(Class.prototype); SubClass.prototype.constructor = SubClass; test1 = new SubClass(); alert("test1 asdf: " + test1.asdf + " " + test1.asdf2); test1.change_asdf(); alert("test1 asdf: " + test1.asdf + " " + test1.asdf2); test2 = new SubClass(); alert("test2 asdf: " + test2.asdf + " " + test2.asdf2); </code></pre>
    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.
    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