base = function () {
this.A = function () {
this.B();
}
var C = function () {
alert('I am C');
}
}
sub = function () {
this.B = function () {
C();
}
}
sub.prototype = new base();
(new sub()).A();
How can I tell the call to B() to evaluate with respect to the base class? (i.e. call c of the base class) Is this impossible?
Normally I would recommend
myFunc.applyandmyFunc.eval; that’s what I interpret as “with respect to the base class” in javascript.However based on your title, you say “inside base class scope”; if I assume correctly that you are talking about closures and being able to refer to variables in outer functions, this is impossible, except perhaps with keeping an entrypoint into the closure into which you can pass in requests eval-style… but if you do that, you might as well have a an object like
this._private.C.If you are attempting to keep things “private”, it is not really worth bothering to do so in javascript.
If you say your motivation for
this.C = function(){...}and access it asthis.C(), I may be able to give a clearer answer.In that case, it really isn’t private. However what you can do is define your helper method where it belongs: appropriately in an outer scope. =) For example: