Question

I am using prototypal inheritance in JavaScript and have hit an issue I can't quite figure out. Many JS inheritance examples show accessing a super's members from a sub, but I need to access the sub's members from the super.

I'm building a tool to performance test mapping web services. I want to support multiple versions of the WMS protocol. I want to keep all shared functionality / properties in a WMS base class wherever possible, and only provide specific version implementation details where necessary. My WMS v1.1.1 function looks like this:

function wms111() {
    this.version = '1.1.1';
}
wms111.prototype = new wms();

My wms function (short version) is as follows:

function wms() {

    var that = this;

    this.HTTPMethod = 'GET';

    this.descriptionParameters = {
        service: 'wms',
        version: that.version,
        request: 'getcapabilities'
    };
}

I then test this with a call like

var service = new wms111();
var descriptionParameters = service.descriptionParameters;

I get the descriptionParameters object with the service and request properties correctly defined, but version is undefined.

Can anyone help me figure out how I access the correct properties from wms111?

Any help much appreciated.

Was it helpful?

Solution

This should make it work as intended:

function wms111() {    
    this.descriptionParameters.version = '1.1.1';
}

Instead of defining a brand new property, just overwrite the property that should be different in the child.

Here it is in action: http://jsfiddle.net/Wd9vE/1/

OTHER TIPS

I could be wrong on this one, but I'm pretty sure you can't. Inheritance works top-down, not bottom-up. You can overload the function in the subclass, but that's essentially treating that function in the superclass as an abstract function, which has to be overloaded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top