Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript accessing class variable from an inherited class method defined in parent
    primarykey
    data
    text
    <p>Well, I am new to prototype programming / design. I would be happy for a help.</p> <p>The question is why my "<code>this.__proto__instances</code>" inside "find" method returns "undefined"? </p> <p>If my approach is wrong, forgive me, I will be happy to know the correct approach for calling a class method to find an element in a class variable array, without having the method to be defined for every child. </p> <p>The problem in details is elaborated as comments in the code below. </p> <p>Thank you.</p> <pre><code>function Attribute(name,type){ //some members definition, including uid, name, and type }; Attribute.prototype.find=function(uid){ var found_attr=false; this.__proto__.instances.forEach(function(attr){ if (attr.uid == uid) found_attr=attr; }); return found_attr; }; </code></pre> <p><code>this.__proto__.instances.forEach(function(attr){</code> above is the erroneous line. Log says "cannot call method forEach of undefined" </p> <pre><code>function ReferenceAttribute(arg_hash){ Attribute.call(this,arg_hash.name,arg_hash.type); //some members definition this.pushInstance(this); }; </code></pre> <p><code>this.pushInstance(this);</code> pushes this instance to ReferenceAttribute.prototype.instances that works fine</p> <pre><code>ReferenceAttribute.prototype=new Attribute(); </code></pre> <p>ReferenceAttribute inherits Attribute with prototype chaining method</p> <pre><code>ReferenceAttribute.prototype.instances=new Array(); </code></pre> <p>Line above declares array containing all instances of reference attributes. For every new object of ReferenceAttribute, it will be pushed into this array, done in a method pushInstance() . The pushing is always successful, I checked them via console logging. The array does contain the ReferenceAtribute instances</p> <pre><code>function ActiveAttribute(arg_hash){ Attribute.call(this,arg_hash.name,arg_hash.type); //some members definition this.pushInstance(this); }; ActiveAttribute.prototype=new Attribute(); ActiveAttribute.prototype.instances=new Array(); </code></pre> <p>use it in the program</p> <pre><code>var ref_attr=ReferenceAttribute.prototype.find("a uid"); </code></pre> <p>gives the error saying that it cannot call method forEach of undefined. It can call method find, so it gets inherited well. But "this._<em>proto</em>_instances" inside find method definition is wrong I guess.</p> <p><strong>EDIT :</strong> </p> <pre><code>Attribute.prototype.pushInstance=function(my_attribute){ this.__proto__.instances.push(my_attribute); }; </code></pre> <p>this function works. Although instances array is possessed by either ActiveAttribute or ReferenceAttribute, and not Attribute itself, but this function does work in pushing it to the array.</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.
 

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