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

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?


By the sound of things, it's impossible. The idea was to automate a code-checker for my students code (I tutor kids) but I'll just have to find another way. Thanks anyway.

Answers


You are mixing Javascript object model with scoped variables which do not interoperate*.

The inherits idiom of doing SubClass.prototype = new BaseClass(); only works when you are using prototypes and constructors naturally:

function BaseClass(arg1, arg2, arg3) {
    this._privateVar = 7500;
}
BaseClass.prototype.getPrivateVar = function() {
    return this._privateVar;
};

function SubClass(arg1, arg2, arg3, privateVar) {

}
SubClass.prototype = new BaseClass();
//Better way to do it is 
//SubClass.prototype = Object.create(BaseClass.prototype);
SubClass.prototype.constructor = SubClass;

Before you argue that anyone can access the property by just writing _, I could argue back that anyone can access any private in Java, PHP or C# by using reflection. Or using instance_eval or send in Ruby and so on. So it's out of your hands anyway.


*None or most of these don't work when you use scoped variables depending on implementation:

  • Enumerability
  • Writability
  • First-class Accessors
  • Sealedness, Frozedness and state of Extension
  • Reflection through getPropertyNames or keys
  • instanceof operator
  • Generic methods
  • Inheritance

Need Your Help

How to delete preferences for Mac application?

objective-c xcode osx cocoa

I'm developing a Mac desktop app using Cocoa and Objective C in XCode. I need to simulate a clean reinstall, but I can't figure out how to completely delete all preferences for the application.

Freeze During Type.GetType() In DbProviderFactories.IncludeFrameworkFactoryClasses() in Entity Framework 5

c# asp.net .net entity-framework

Local execution of my Entity Framework 5 ASP.NET MVC application suddenly stopped working today. It starts up and then appears to be thinking for a long time (I've let it run as long as 2 hours). E...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.