Javascript: Inherit method from base class and return the subclass's private variable

Posted by marisbest2 on Stack Overflow See other posts from Stack Overflow or by marisbest2
Published on 2013-08-02T15:25:42Z Indexed on 2013/08/02 15:36 UTC
Read the original article Hit count: 114

Filed under:
|
|

I have the following BaseClass defined:

function BaseClass (arg1,arg2,arg3) {
    //constructor code here then - 
    var privateVar = 7500;
    this.getPrivateVar = function() { return privateVar; };
}

I want to have the following subclass which allows changing privateVar like so:

function SubClass (arg1,arg2,arg3,privateVar) {
    //constructor code here then - 
    var privateVar = privateVar;
}
SubClass.prototype = new BaseClass();

Now I want SubClass to inherit the getPrivateVar method. However, when I try this, it always returns 7500 which is the value in the BaseClass and not the value of privateVar.

In other words, is it possible to inherit a BaseClass's public methods but have any references in them refer to the SubClass's properties? And how would I do that?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about oop