Javascript and prototype chaining (methods and vars not being looked up at prototypes)
<p>I finished reading "Object Oriented Javascript". I'm following this code to "inherit" and object from another:</p>

<pre><code>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;

    }
}
</code></pre>

<p>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:</p>

<p>Think that ClassB construction creates a var called myVar.</p>

<pre><code>deriveFrom(ClassA,ClassB);
var obj=new ClassA();
</code></pre>

<p>This creates a prototype chain like this:</p>

<p>obj-> prototype (function)->prototype (ClassB) -> myVar.</p>

<p>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?</p>

<p>if I do obj.prototype.myVar it finds it :S.</p>

<p>Could anybody help please?</p>

<h3>Update:</h3>

<pre><code>function ParentClass() 
{
}

ParentClass.prototype.initWithAAndB=function(a, b)
{
    this.a = a;
    this.b = b;
}


function ChildClass()
{
}

//Inheritance
deriveFrom(ChildClass,ParentClass);

ChildClass.prototype.initWithAAndBAndC=function(a,b,c)
{
    this._super.initWithAAndB(a, b);

    this.c = c;
}


var a = new ChildClass();
a.initWithAAndBAndC(1, 2, 3);

//This works
a.prototype.initWithAAndB(7, 8);

//This does not work, but as the book I stated before explained the function should be found 
a.initWithAAndB(7,8);
</code></pre>
