0

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.

2 Answers 2

2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent thanks a lot. Can you explain what's going on when it doesn't work? I thought that the new wms111() object would be treated as a single entity that just unions everything in both wms() and wms111() functions, so using that (this) wouldn't differentiate between them
0

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.

1 Comment

that possibility did occur to me, but then with JavaScript I've come to expect to be able to do stuff that doesn't make sense :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.