Well, I am new to prototype programming / design. I would be happy for a help.
The question is why my "this.__proto__instances" inside "find" method returns "undefined"?
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.
The problem in details is elaborated as comments in the code below.
Thank you.
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;
};
this.__proto__.instances.forEach(function(attr){ above is the erroneous line. Log says "cannot call method forEach of undefined"
function ReferenceAttribute(arg_hash){
Attribute.call(this,arg_hash.name,arg_hash.type);
//some members definition
this.pushInstance(this);
};
this.pushInstance(this); pushes this instance to ReferenceAttribute.prototype.instances that works fine
ReferenceAttribute.prototype=new Attribute();
ReferenceAttribute inherits Attribute with prototype chaining method
ReferenceAttribute.prototype.instances=new Array();
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
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();
use it in the program
var ref_attr=ReferenceAttribute.prototype.find("a uid");
gives the error saying that it cannot call method forEach of undefined. It can call method find, so it gets inherited well. But "this._proto_instances" inside find method definition is wrong I guess.
EDIT :
Attribute.prototype.pushInstance=function(my_attribute){
this.__proto__.instances.push(my_attribute);
};
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.