Hi,
I finished reading "Object Oriented Javascript". I'm following this code to "inherit" and object from another:
function deriveFrom(child,parent)
{
if (parent == this)
alert("You can't inherit from self class type")
else
{
var f = function () { }
f.prototype = parent.prototype;
child.prototype = f;
child.prototype._super = f.prototype;
child.constructor = child;
}
}
The problem I encountered, is when I want to access a function or var that is not defined in the child class, but it is in the parent class, and example:
Think that ClassB construction creates a var called myVar.
deriveFrom(ClassA,ClassB);
var obj=new ClassA();
This creates a prototype chain like this:
obj-> prototype (function)->prototype (ClassB) -> myVar.
If I do something like a.myVar I get an undefined. Why? The book states that javascript will look for the var through prototypes until it gets it. So, first it will search for it at obj, not found it will get its prototype object that it is a function, it won't find it, then it will continue going down into the function prototype, and there it will find myVar. Isn't this the process?
if I do obj.prototype.myVar it finds it :S.
Could anybody help please?
Thanks in advance, Mathew.