mutated 'this' in javascript super call, how to deal with?
<p>Sorry if it's hard to explain. 
Suppose class Y extends class Z, and class X extends class Y</p>

<p>The thing is if a class doesn't have a method, it calls it's super class, so far so good.</p>

<p><code>X.prototype.v = function{return this.getV();} //getV() doesn't exist in class X</code></p>

<p>since class X extends class Y and getV() exist in class Y, the call goes here:</p>

<pre><code>Y.prototype.getV = function{return this.parent().getV()+1} 
</code></pre>

<p>the working function parent() returns an instance of it's super class. Suppose z also has a method getV, which returns a real int value. </p>

<pre><code>Z.prototype.getV = function{return 1}
</code></pre>

<p>So the function getV() in class Y is mean to return the getV value from Z plus 1, and send it to the lowest class X.</p>

<p>Most wired part is here. As long as the method getV is called from X.v(), 'this' in y.getV() refer to X, not Y!</p>

<p>so the function getV() in Y becomes X.parent().getV()+1, and I get 'maximum call stack size exceeded'</p>

<p>A stupid but extremely effective way to solve this, is to write </p>

<pre><code>this.parent().parent().getV()+1
</code></pre>

<p>double parents make the sender Z not y, then it returns 2 when calling X.getV()</p>

<p>It's stupid because if the caller is Y itself, like Y.getV(), I think then 'this' correctly means Y here, then there're too many parent() calls, leading it to undefined.</p>

<p>Some thoughts like we can get rid of 'this', and use another way to get current class. Less desirable way might be keeping track of all functions for all classes, and set the correct number of parent(). I believe there's more. However, none of them was tested out yet.</p>

<p>A minimal code example, taken from the pieces of code above, can be:</p>

<pre><code>class Z{

}
class Y extends Z{

}
class X extends Y{

}

X.prototype.v = function{
    return this.getV();
}

Y.prototype.getV = function{
    return this.parent().getV()+1;
}

Z.prototype.getV = function{
    return 1;
}

var x = new X();
console.log(x.v());
</code></pre>