Component 10
Component 10

Reputation: 10477

Simple 'static' in Javascript 'base class'

I've read through most of the Javascript inheritance references, but I'm afraid I'm still scratching my head as to how to do this.

I'm trying to put together several classes, in the general sense, that will have similar behavior and thought using prototypes might be a way to achieve that. So I create a base class as follows:

function my_base_class()
{
    var a_common_object = undefined; // The true value of this can't be set until runtime.

    // some other stuff ...
};

Ideally, I'd like a_common_object to be private or at least protected, but just getting it working would be a good first step. I then need to create several derived classes of which this might be one:

function my_derived_class()
{
    this.do_something_to_common_object = function()
    {
        // Here I need to reference my_base_class.a_common_object but
        // at this point there's no relationship between the two classes
    };
};

I now set the prototype of my_derived_class while creating an instance:

my_derived_class.prototype = new my_base_class();
var my_derived_class_inst = new my_derived_class();

So at this point I'm hoping that I have an object - my_derived_class_inst which has traits of my_base_class including the static object a_common_object which I can access.

I have two questions:

  1. How do I refer to a_common_object within my_derived_class when there's no relationship established between the two classes?
  2. How can I then change a_common_object to its true value, so that all derived classes seamlessly pick up the new value.

Please don't simply refer me to the standard reference web sites on inheritance as I've read most of them through and I'm still no wiser. It seems to me that the answer should be really simple but so far it escapes me. Many thanks.

Upvotes: 1

Views: 55

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123423

do_something_to_common_object() really doesn't have a way of reaching a_common_object directly.

a_common_object isn't a member of the instance created for the prototype. It's a local variable scoped inside the constructor. So, only a function that's also defined within the constructor can reach it (ref: closures):

function my_base_class()
{
    var a_common_object = undefined;

    Object.defineProperty(this, 'a_common_object', {
        get: function () {
            return a_common_object;
        }
    });

    // ...
}
function my_derived_class()
{
    this.do_something_to_common_object = function()
    {
        console.log(this.a_common_object); // uses getter to retrieve the value
    };
};

It would still be publicly accessible, but your options are limited as JavaScript doesn't yet support or have an equivalent to access modifiers.

Though, with Object.defineProperty(), it would at least be read-only so far and non-enumerable by default (won't appear in a for..in loop).

At least until #2, where you'd need to also have a setter. Though, it would be a chance to validate the value being storing it.

Object.defineProperty(this, 'a_common_object', {
    // ....

    set: function (value) {
        if (/* validator */) {
            a_common_object = value;
        }
    }
});

Upvotes: 2

Related Questions